A basic web server with Node.JS and Express

Here is a memo on how you can create a simple web server delivering static content with Node.js and and Express. The content could be html files, js, css or any other kind of files that doesn’t require server processing (like PHP, C#, Java, …).

But why Node.js instead of using Apache or IIS? Like you are going to see in the few lines below creating a web server with Node.js takes only few lines of code and can be up and running in 30 seconds (max!). When your purpose is only to test things the configuration of a « traditionnal » web server can be overkill.

A couple of days ago I played with the WebSocket API (HTML5 rocks!) by creating a WhiteBoard that can be used by several users at same time (see it on Github). With Node.js and Express I could quickly setup a web server that delivery html, css and javascripts files to my test users over the network. And everyone that want to pull source code and test the project can easily execute the embedded web server directly.


  1. The first thing you have to do is to install Node.js if it’s not already. Just download it from the website (see here), click, click, next and it’s completed!
  2. Open the Node.js command prompt, go in the directory in which you want to create the server and install Express (which is the middleware that we use to create the webserver):
    $> npm install express
  3. Create the web server:
    var express = require('express');
    var app = express();
    app.configure(function () {
            "/", //the URL throught which you want to access to you static content
            express.static(__dirname) //where your static content is located in your filesystem
    app.listen(3000); //the port you want to use
  4. Execute your Web Server:
    $> node server.js
  5. Your web server is reachable through http://localhost:3000/



Une réflexion sur “A basic web server with Node.JS and Express

  1. Looking at this in 2015: app.configure is deprecated since version 4x – so code works, but without lines 3&8. What did .configure get replaced with? ..set?
    Anyways: Got my files served where I needed them, merçi beaucoup 🙂

Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion /  Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion /  Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion /  Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion /  Changer )


Connexion à %s