Write a simple http-server using nodejs
by Jerad Rutnam -
This an quick guide to create an http web server using nodeJS.
Download and Install NodeJS
If you haven’t installed Node yet, download the latest stable release of NodeJS from https://nodejs.org and install using all the default options.
Create a package.json
file in your website root folder. And add the following,
{
"name": "<project-name>",
"version": "1.0.0",
"url": "<project-documentation-url>",
"git": "<project-github-repo-url>",
"private": true,
"designer": "<author-name>",
"designerURL": "<author-website-url>",
"license": "Apache License, Version 2.0",
"licenseURL": "<project-licesnse-url>",
"scripts":{
"start": "node index.js"
},
"dependencies": {
"express":"^4.15.3",
"copy": "0.3.0"
}
}
JavaScript (jQuery)
var express = require('express');
var app = express();
var http = require('http');
var fs = require('fs');
var cwd = '';
var port = 9001;
app.use('/', function (request, response) {
console.log('Received request for URL: ' + request.url);
var filePath = request.url.split("?").shift()
if (filePath == '/') {
filePath = cwd + '/index.html';
}
else {
filePath = cwd + filePath;
}
fs.readFile(filePath, null, function(error, data){
if(error){
response.writeHead(404);
response.write('File not found.');
}
else{
response.writeHead(200);
response.write(data);
}
response.end();
});
});
console.log('Server address: http://127.0.0.1:' + port + '/');
console.log('Server running... press ctrl-c to stop.');
app.listen(port);
Run
Navigate your project root folder on console/terminal and run,
npm install
And then
npm start