My first little Node.js App

January 24th, 2013 by admin Leave a reply »

If you are a developer and you haven’t heard about node.js, what rock have you been living under??  Anyways, if you have been hearing about it and would like to learn more, head over to their site HERE and download it and start playing with it.  Its super easy to get going with node, once you install it, all you have to do is open a terminal window and type node “name_of_file.js”, with the correct javascript in it, of course.

I just wrote up this quick little example to see if I could get node to act like a webserver, so here is the js code for it below:

var http = require('http');
var fs = require('fs');
http.createServer(function (req, response) {
    fs.readFile('index.html', function (error, content) {
        if (error) {
        else {
            response.writeHead(200, { 'Content-Type': 'text/html' });
            response.end(content, 'utf-8');

}).listen(1337, '');
console.log('Server running at');

Just create a directory anywhere on your system and create a file with index.html (That’s my example, it can be anything) and then simply issue this command in the terminal window.

node “name_of_file.js”

My example is this: node server.js.

You will get a ‘Server running at’ message after that and that will let you know that node is running correctly, all you have to do after that is open a web browser and go to the address of and you should have the webpage you built, you may want to just slap some text in there so you see something.

I hope this helps someone, I plan on delving into the node realm a lot more in the coming weeks, so look for other posts down the road.


Leave a Reply