Minimal Express Server

Let’s go over how to set up a minimal Express server. You’ll need Node.js installed.

First install express:

$ npm install express

Then create server.js:

$ touch server.js

Import the module and initialize your app:

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

Then create the index route:

app.get('/', function(request, response, next) {
    response.send('<h1>Hello, world!</h1>');

Make sure that your server is listening to a port:

var PORT = 3000;
app.listen(PORT, function() {
    console.log('http://localhost:' + PORT);

And now you can run the server:

$ node server.js

Your site should be up at http://localhost:3000 (open the url in a browser). And that’s it!

If you enjoyed this post, please consider supporting this site!