Node.js

node.js logo

Have been playing around with Node.js a lot lately. I like what I see. The essential feature of Node is writing JavaScript on the server side and not just on the client. There’s a lot of sense to that. But there’s also a bit more to it. Making use of the whole asynchronous approach that JavaScript lends itself so well to, is also a feature that I’m liking here.

All the beginner tutorials seem to demonstrate something around the creation of a simple web server. And in my playing arounds I am no different.

//load the http object
var http = require("http");
//load the file system object
var fs = require("fs");

http.createServer(function(request, response) {

	if (request.url.indexOf('index.html') > 0) {
		fs.readFile("index.html", 'utf-8', function (error, data) {
			console.log('reading file');
			response.writeHead(200, {
				'Content-Type' : 'text/html'
			});
			//write out the contents of the html file
			//to the response stream
			response.end(data);
		});
	}else{
		console.log("not index.html");
	}

}).listen(8080);

console.log("Server has started.");

Share this Post:
comments powered by Disqus