Using Node.js and Express.js as a lightweight HTTP Server

Alright, we all have many ways of getting lightweight, quick and drity web servers. In the .net world, IIS Express is used and in Python 2.x, SimpleHTTP is used. We all heard how awesome node.js is as a web server and how easy it is to get rocking with it. However, it serving files is requires a lot more jiggery pokery than writing a "Hello World" string.

This guide will show you how to make a simple http server that serves static files for simple stuff.

Prerequisites:

  1. Node.js runtime is installed. If for some strange reason you are reading this, want to use node.js and do not have node.js installed, here is the link : http://nodejs.org/
  2. Create a directory where u want the web server to run. For the purposes of this guide the folder is named jiggery_pokery in the root directory.
  3. Follow the steps as outlined below.
Step 1 : Go to directory
C:\> cd jiggery_pokery
Step 2 : Initialize NPM packages
C:\jiggery_pokery> npm init

Just hit enter for everything, for the purposes of this somewhat quaint HTTP server, you wont need to make this all proper.

Step 3 : Install Express Framework

While it is entirely possible to do this without using the Express Node.js framework, I can't be bothered to do it as the code is a tad more verbose. Anyway, I digress, here is the command as ordained by your's truly:

C:\jiggery_pokery> npm install express --save

*note: the --save parameter is meant to ensure that the requires express framework dependencies are stired within your directory so that you would be able to copy this to another machine without worrying about this dependency being there.

Step 4 : Setup & Write JavaScript :)

Here comes the fun part, withing the directory, create a file that will act as the script to bring this all together. For the purposes of this guide, I will call it index.js.

After that is done, create a folder to store your static files, I will call it static as I am too lazy to come up with anything more creative. So, if you are still in your command line interface, do this:

C:\jiggery_pokery> mkdir static

Once those 2 things are done, open up with the JS file with an editor of choice, be it VIM, Sublime, Atom or < insert your favourite editor here >. Now its time to write some code as seen below:

"use strict";

/*
    Load Dependencies
*/
var express = require("express");
var path = require("path");

// Instace of an express.js app
var app = express();

// specifies this directory is cleared for serving static files
app.use(express.static(path.join(__dirname, "static")));

var server = app.listen(3000, function () {
    var host = server.address().address;
    var port = server.address().port;

    console.log('Example app listening at http://%s:%s', host, port);
});

Now you can shove in whatever static file be it a .jpg, .html or whatever into the static folder we created earlier.

Endgame

Now we got all the scaffolding done, just do the following:

C:\jiggery_pokery> node index.js

Now you should have your HTTP server running. Lets just say you have something named test.html in the static folder, in order to browse it, enter the following URL:

http://localhost:3000/test.html

That will now serve the HTML file you created. This is a really simple way of getting the ball rolling. You can also create folders and it will behave as though this is IIS Express/Apache/SimpleHTTP.

Have fun :)