Node.js – HTTP Server


Setup Node HTTP Server

One of the most common uses for Node is as a web server for applications built with Angular or Express JS (see Advanced Node section). The code below configures a simple server:

var http = require('http');
var server = http.createServer(function onRequest(request, response) {
response.writeHead(200, { 'Content-Type': 'text/plain'});
response.write('Welcome to a simple HTTP Server'); response.end();

The first line includes the HTTP module. Like other frameworks, Node includes a set of built-in modules that perform specific tasks. You can think of a module as a namespace in .NET or a package in Java. The require statement imports module. Then we create a server instance using the create Server function. If you are a Java programmer, the syntax can be very similar to setting Java Servlet. Callback function containing request and response objects is passed as a parameter to the main function. Finally, we create a simple header and write out some content to stream. Once launched from your IDE, or on the node command line, you should see that the server is running, as indicated by the blinking the cursor is on the command line, which means the server is waiting for requests. If you open your browser and go to localhost: 3000, you should see a welcome the message displayed from the above example.

So how do you implement application pages using a Node server? For this we use modules http, fs, path and url. The fs module is a file system module is responsible for streaming files in Node. An example of loading pages below:

var http = require('http');
var fs = require('fs');
var path = require('path');
var url = require('url');

var server = http.createServer(function onRequest(request, response) {
var urlParts = url.parse(request.url);
var page = 'pages' + urlParts.pathname;
fs.exists(page, function fileExists(exists) {
if (exists) {
response.writeHead(200, { 'Content-Type': 'text/plain'}); fs.createReadStream(page).pipe(response);
} else {
response.write('Page Not Found');

In the above example, we are doing another callback function to check if exists, and if so, we pass the content of the file to the browser, otherwise we send a message that the page cannot be found. You can use the above code for most of your application hosting if your application was simple and not using an application architecture like MVC. If your application was large then the above code will have to be repeated in different ways to process in various ways, etc. Node helps to solve this problem with a concept called Connect middleware (CM). If you are working with Java or .NET you CM can be thought of as filters. Like filters, CM is code that is inserted into a request pipeline to perform specific functions. Some CM examples are for Logging and Authentication. CM is built on top of the host HTTP server and refer to it through the name of the connection package module in your application. To add CM to your Node modules, go to command line and type npm connect. The code below shows an example using CM:

var connect = require('connect')
, http = require('http');
var app = connect()
.use(function(req, res){
res.end('Hello from Connect!\n');

From the example above, you can see that the application variable is being passed to createServer, adding almost all CM functionality. Differently write code with CM, but with more flexibility, e.g .:

var connect = require('connect');
var intercept = function(request,response,next){
var server = connect()
.use(function onRequest(request, response) {
response.write("Hello from the Connect Middleware Package"); response.end();

The big difference between this example and the previous one is that we create a function called intercept that also handles the request and response, but also passes a parameter named next. After that, we configured the server with connect the packet and tell it to use the intercept function before processing requests as in the other examples, using only the http module. If you write something in the console in the intercept function, this will be displayed in console before the Hello from message appears in the browser. You can you will probably start to understand how to write code to handle it as efficiently as possible several operations using this example.