Run a Simple Web Server with Express.js

Recently I've had the opportunity to take on some side projects and I thought it would be pretty cool to leverage more modern programming stacks. For one there was Node.js and it was something that always intrigued me, however that was just one part of the equation. Traditionally LAMP/LEMP is the more familiar go-to suite for development, so I looked into recent popular solutions. I decided to try out MEAN (MongoDB, Express, AngularJS, Node.js) as Mongo was already familiar to me and it would be quite interesting to be able to use JavaScript on both client and server sides.

One of the first things to do was to host a simple static page to start out rather than leaving the domain "empty". Personally I like leveraging existing frameworks or libraries when it comes to development. For this Node app, I'm using Express to host a simple site under the /public directory as seen below:

var express = require('express'),
    http = require('http'),
    app = express(),
    httpServer = http.createServer(app);
app.set('port', 80);
app.use(express.static(__dirname + '/public'));
app.get('*', function(req, res, next) {
  var err = new Error();
  err.status = 404;
// Handling 404 errors
app.use(function(err, req, res, next) {
  if(err.status !== 404) {
    return next();
  res.send(err.message || '404 Not Found');
httpServer.listen(app.get('port'), function () {
    console.log("Express server listening on port %s.", httpServer.address().port);

There's also some basic error handling in the case where users stumble upon non-existent directories. If your web app connects to a database then handling 500 errors should also be considered as well.

Note that anything under port 1000 requires root privleges to run so using something like Nginx as a reverse proxy would be ideal.

Got any tips or improvements? Feel free to leave them below.