1. Part 1: Introduction to NodeJS
  2. Part 2: NPM and modules in NodeJS

This will be a very long post, so I’m dividing it further to make it easier.

ExpressJS is a great utility for Node, it can be precisely defined in its own words ” a minimal and flexible node.js web application framework”. So it is clear that ExpressJs is a module that acts as a web framework for NodeJS. Going through my books, I HAD to read about it, but the very first question after picking up the project was ” Do I really need Express ?” This will be a very long post, so I’m dividing it further to make it easier.

To answer this question let’s go back to basics. How do you actually render content on a web browser ? Ignoring what happens on the client side that is the parsing of HTML stuff, You’ve a very cool language ( say PHP ) and an equally cool web server ( Apache ) but how does all that networking stuff work behind your back ? HTTP is a Application protocol which is heavily used in this process, but do you really have idea what it does behind the scene ? Of course we don’t have to bug our mind with that, the whole point of Abstraction is simple, you should know how to code, your web server should understand how to display it !

We use NodeJS to create web servers using TCP/HTTP/UDP Protocols, a very simple http server can be initiated in Node through following snippet.

So is that all required to create a web server ? How cool is that ?  Practically only 2 lines of code made an HTTP Server, climb over, this is just the beginning of fun ! lemme explain the code first.

http is a core module which we are using here, it allows you to create a server through the createServer(fn(request,response)) function. Wait a minute what is this fn(request,response) ?  Well this is the interesting part of Node ! Asynchronous Programming, and what is that ? Well Asynchronous Programming is totally left for another blog post, till then we can explain fn() as a callback function, whenever createServer will be initiated, it’ll call fn().

request is a request that comes from the client. In many apps, you’ll see this shortened to req.
The response is the next part. Just like the prior argument is often shortened, this is often shortened to the three-letter res.writeHead() is used to send Header-Content to the web-browser ( here we are sending a HTTP 200 code which mean success). With each response, you get the response all ready to send, and then you call response.end. Eventually, you must call this method; even the Node docs say so. This method does the actual sending of data. You can try making a server where you don’t call it, and it just hangs forever.

So its very simple, every time a call ( lets call it an event ) is made to createServer(), it’ll respond back with some action !
Save this file with the name server.js and fire it with command node server.js

Point your browser to http://localhost:5555 and viola ! it works. Now try to fiddle with it a little bit try firing something like http://localhost:5555/something_random or http://localhost:5555/?query=random. You’d find the result is still the very same string, so what is happening here ? we’ve been able to initiate a web server but our server seriously doesn’t know how to respond with different URLs or GET request ( Technically this is called routing )

So to handle such things we technically need a Routing Handler , we can make our own handler, how do we do that ? think about that , we need to “capture” the url or the “method” used to call that url and then we should send back an appropriate response. Lets just save it for another day !