Node.js express introduction

Introduction

The Express framework (http://expressjs.com/) is a third-party extension for the HTTP module which improves the creation/management of Node.js servers

The Express app .get method allows you to define callback methods for specific routes.

var express = require("express"),
  fs = require("fs");
var config = JSON.parse(fs.readFileSync("files/config.json"));
//Create server// w w w. j  ava  2  s. c  o m
var app = express();

//Make sure Express uses .get method callbacks before loading a static page
app.use(app.router);
//Define public directory for static files (ie. views)
app.use(express.static(__dirname + "/views"));

//Define routes: the first route the request url matches will be called
app.get("/", function(request, response) {
  //The Express .send method functions similar to response.end
  response.send(fs.readFileSync("views/index.html").toString());
});

//colons are used to segment parts of the request url as variables
app.get("/getvar/:text", function(request, response) {
  response.send("The url segment after /getvar/ is: " + request.params.text);
});

//Define a final catch-all for all routes which don't apply and display a 404 page
app.get("*", function(request, response) {
  //The .send method accepts an optional, second parameter for a status-code
  response.send(fs.readFileSync("views/404.html").toString(), 404);
});
//Start server (listening for HTTP requests)
/* The server.listen method assigns the server a port and host to listen for HTTP requests */
console.log("Setting server to listen at " + config.host + ":" + config.port);
app.listen(config.port, config.host);



PreviousNext

Related