25 Aug 2013

Hello World - Express and node.js

A simple node.js / Express application that serves an HTML page.

If you haven't already, install the Express module using npm:

$ sudo npm install express

Edit server.js:

var express = require("express");
var app = express();

app.get("*", function(req, res) {
  var html = "<!DOCTYPE html>"
           + "<html><head>"
           + "  <meta charset='utf-8'>"
           + "  <title>Hello World</title>"
           + "</head><body>"
           + "  <h1>Hello World</h1>"
           + "  <p>Simple node.js + Express application</p>"
           + "</body></html>";
  res.send(html);
});

app.listen(8080);

console.log("Server listening on port 8080");

Start the server:

$ node server.js
Server listening on port 8080

Test in your browser:

http://localhost:8080
Hello World
Simple node.js + Express application

For a standalone node.js example look at Hello World.