A simple static page web server using node js

nodejs-dark

I’m building a SPA using AngularJS, and Chrome doesn’t allow Angular to load the html partials because of cross origin request errors.  Firefox works, but I just prefer to develop using Chrome, so I needed to host the SPA somewhere.  IIS seemed like overkill, so I turned to Node which turned out to be very simple.

  1. Assuming you have node and NPM installed, install connect and serve-static with NPM
    $ npm install connect serve-static
  2. Create a server.js and place it in the same directory as the entry point of your SPA
    var connect = require('connect'); 
    var serveStatic = require('serve-static');
    connect().use(serveStatic(__dirname)).listen(8080);
  3. Start the web server
    $ node server.js
  4. Browse to your SPA entry page:
    http://localhost:8080/index.html

 

That’s it!  I really love some things about Node, and the simplicity of setting up a web server is one of those things.