Tutorials‎ > ‎

Web Hosting using Node.js

posted Dec 22, 2011, 10:38 PM by Reza Aggi Prasetyo   [ updated Aug 15, 2016, 11:39 PM by Surya Wang ]

Node.js

Node.js is a driving project of SSJS that based on V8 JavaScript engine from Google Chrome. Node.js can be downloaded at its official site. It also contains the manual and documentation of Node.js. This article uses the v0.5.9 of Node.js windows executable program that can be downloaded here.

Hosting a Website

Node will response the same for each URL request that sent by clients. In order to make a Node application that can reponse any requests given with HTML pages based on the URL given, the application must have the ability to browse the HTML page requested and return it as a response. Create a "Server.js" file, then insert this following code.
var http = require('http'),
url = require('url'),
sys = require('util');

var handler = require('./lib/Handler.js');

http.createServer(function(request, response) {

try {
console.log('Request from: ' +
request.connection.remoteAddress +
' | href: ' + url.parse(request.url).href);

handler.handle(request, response);

} catch (e) {
sys.puts(e);
response.writeHead(500);
response.end('Internal Server Error');
}

}).listen(8087, "127.0.0.1", function () {
console.log("Server running at http://localhost:8087/");
});
 

http.createServer()
is one of Node basic method to start a server instance. In the example above, I use port 8087.
.listen(8087, ...)
 

This program will handle every request that received from clients with the handle function that located in Handler library.
handle(request, response)


You also need to create "Handler.js" under directory 'lib'.
var fs = require('fs'),
path = require('path');

this.handle = function (request, response) {
var renderHtml = function (content) {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(content, 'utf-8');
};

var notFound = function () {
response.writeHead(404, { 'Content-Type': 'text/plain' });
response.end("404 Not Found\n");
};

var parts = request.url.split('/');
var rest = parts.slice(2).join('/');
var page_rest = parts.slice(1).join('/');

if(request.url == '/') {
fs.readFile('./web/index.html', function (error, content) {
if(error) {
notFound();
return;
}
else {
renderHtml(content);
}
});
}
else if(parts[1] != 'css' && parts[1] != 'images' && parts[1] != 'js') {
fs.readFile('./web/' + page_rest, function (error, content) {
if(error) {
notFound();
return;
}
else {
renderHtml(content);
}
});
}
else {
path.exists('./web/' + parts[1] + '/' + rest, function(exists) {
if(!exists) {
notFound();
return;
}

fs.readFile('./web/' + parts[1] + '/' + rest, 'binary', function(error, content) {
response.writeHead(200);
response.end(content, 'binary');
});
});
}
};


Handler.js will handle the request from client based on the URL given. The URL will be separated by '/' character and the first word will be checked. If the URL is '/', Handler.js will return 'index.html' file that located in 'web' directory as an HTML. If first word is not 'css', 'images', or 'js', it will be returned as a binary file, and other else will be returned as an HTML file.

ċ
SSJS_1.zip
(5k)
Reza Aggi Prasetyo,
Dec 22, 2011, 10:38 PM