JavaScript Node Server - tdkehoe/blog GitHub Wiki

To run a JavaScript program from the terminal:

node app.js

To start a server, your package.json should contain this script:

  "scripts": {
    "start": "nodemon --harmony app.js"
  },

You can then enter npm start in your terminal (assuming that your server is called app.js).

A basic server looks looks like this:

var http = require('http'),
fs = require('fs');

var server = http.createServer(function(req, res) {
  var template = fs.readFileSync('./index.html');
  ...
  res.end(template);
})

server.listen(2001, function() {
  console.log("Server running on 2001.");
});