To assign middleware to all routes, you can implement the use()
method on the Express app
object. The use()
method has the following syntax:
use([path], middleware)
The path
variable is optional and defaults to /
, which means all paths. The middleware
parameter is a function that has the following syntax, where req
is the Request
object, res
is the Response
object, and next
is the next middleware function to execute:
function(req, res, next)
Each of the middleware components has a constructor that returns the appropriate middleware function. For example, to apply the body-parser
middleware to all paths with default parameters, you use the following statements:
var express = require('express'),
var bodyParser = require('body-parser'),
var app = express();
app.use('/', bodyParser());