This is an introduction post to node.js..

Node.js is an event driven, non blocking (async) I/O style software that is used to develop server side implementations. (If you’re a Python fan, it’s like programming in Twisted!). It’s also built on Google’s V8 JavaScript engine. Like other event driven servers, Node.js, runs an event loop and handles the events asynchronously with callback invocations.

A typical ‘ hello world ‘ web server implementation can be found below. Run as: node helloworld.js

var http = require('http');
var server = http.createServer(function(request, response) {
response.write("Hello World");
console.log("Server Started")

view raw


hosted with ❤ by GitHub

In this example,

1. we import module http and create a http server that listens on port 8888.

2. When the user makes a Get request on http://localhost:8888, the web server renders Hello World on the web browser. You can play around with request and response variables as used in the server code.

3. It’s interesting to note that if reponse.end() statement is commented out, the request doesn’t complete and the server hangs. If you press Ctrl+C, only then the request completes and ‘Hello World’ is rendered on the browser. So server developers, beware!

You may ask, what’s this function(request, response) and why it’s anonymous? Well, that’s a motivation for you to read my next post! 🙂

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.