Build Your First Node.js Website

Using Express and Nodemon

Last time we made a server using Node's built in HTTP modules. Let's see how we do that same thing, but using Express

 // server.js
var express = require('express');
var app = express();
var port = 8080;

// start the server
app.listen(port, function() {
  console.log('app started');
});

// route our app
app.get('/', function(req, res) {
  res.send('hello world!');
});

Start the server with node server.js and check it out at http://localhost:8080.

Now if you make a change to your 'hello world' string and refresh, you'll notice the changes didn't take. We actually have to restart the server everytime we make a change. That's no fun, so let's install a tool to help.

npm install nodemon --save

Now run nodemon server.js and you never have to restart again!