Express JS Routing


Routing refers to the definition of end points (URIs) to an application and how it responds to client requests.

A route is a combination of a URI, a HTTP request method (GET, POST, and so on), and one or more handlers for the endpoint. It takes the following structure app.METHOD(path, [callback...], call back), where appis an instance of express, METHOD is an HTTP request method, path is a path on the server, and callback is the function executed when the route is matched.

The following is an example of a very basic route.

	  
	var express = require('express');
	var app = express();
	// respond with "hello world" when a GET request is made to the homepage
	app.get('/', function(req, res) {
		res.send('hello world');
	});

Route methods

A route method is derived from one of the HTTP methods, and is attached to an instance of express.

The following is an example of routes defined for the GET and the POST methods to the root of the app.

	  
	// GET method route
	app.get('/', function (req, res) {
		res.send('GET request to the homepage');
	});

	// POST method route
	app.post('/', function (req, res) {
		res.send('POST request to the homepage');
	});

Express supports the following routing methods corresponding to HTTP methods:

get, post, put, head, delete, options, trace, copy, lock, mkcol, move, purge, propf ind, proppatch, unlock, report, mkactivity, checkout, merge, m-search, notify, subscrib e, unsubscribe, patch, search, and connect.

Route paths

Route paths, in combination with a request method, define the endpoints at which requests can be made to. They can be strings, string patterns, or regular expressions.

Express uses pathtoregexp for matching the route paths; see its documentation for all the possibilities in defining route paths. Express Route Tester is a handy tool for testing basic Express routes, although it does not support pattern matching. Query strings are not a part of the route path.

Examples of route paths based on strings:

	  
	// will match request to the root
	app.get('/', function (req, res) {
		res.send('root');
	});

	// will match requests to /about
	app.get('/about', function (req, res) {
		res.send('about');
	});

	// will match request to /random.text
	app.get('/random.text', function (req, res) {
		res.send('random.text');
	});

Examples of route paths based on string patterns:

	  
	// will match acd and abcd
	app.get('/ab?cd', function(req, res) {
		res.send('ab?cd');
	});

	// will match abcd, abbcd, abbbcd, and so on
	app.get('/ab+cd', function(req, res) {
		res.send('ab+cd');
	});

	// will match abcd, abxcd, abRABDOMcd, ab123cd, and so on
	app.get('/ab*cd', function(req, res) {
		res.send('ab*cd');
	});

	// will match /abe and /abcde
	app.get('/ab(cd)?e', function(req, res) {
		res.send('ab(cd)?e');
	});

Route handlers

You can provide multiple callback functions that behave just likemiddleware to handle a request. The only exception is that these callbacks may invoke next('route') to bypass the remaining route callback(s). You can use this mechanism to impose preconditions on a route, then pass control to subsequent routes if there’s no reason to proceed with the current route.

Route handlers can come in the form of a function, an array of functions, or various combinations of both, as shown the following examples.

A route can be handled using a single callback function:

	  
	app.get('/example/a', function (req, res) {
		res.send('Hello from A!');
	});

A route can be handled using a more than one callback function (make sure to specify the next object):

	  
app.get('/example/b', function (req, res, next) {
	console.log('response will be sent by the next function ...');
	next();
}, function (req, res) {
	res.send('Hello from B!');
});

A route can be handled using an array of callback functions:

	  
	var cb0 = function (req, res, next) {
		console.log('CB0');
		next();
	}
	var cb1 = function (req, res, next) {
		console.log('CB1');
		next();
	}
	var cb2 = function (req, res) {
		res.send('Hello from C!');
	}
	app.get('/example/c', [cb0, cb1, cb2])