The heart of an Express application
Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle.
// Here is an example of a simple “Hello World” Express
// application, for which you will define two middleware functions:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.listen(3000);
The following figure shows the elements of a middleware function call:
// This example shows a middleware function with no mount path.
// The function is executed every time the app receives a request.
var app = express();
app.use(function (req, res, next) {
console.log('Time:', Date.now());
next();
});
// This example shows a middleware function mounted
// on the /user/:id path. The function is executed for
// any type of HTTP request on the /user/:id path.
app.use('/user/:id', function (req, res, next) {
console.log('Request Type:', req.method);
next();
});
// This example shows a route and its handler
// function (middleware system). The function
// handles GET requests to the /user/:id path.
app.get('/user/:id', function (req, res, next) {
res.send('USER');
});
Router-level middleware works in the same way as application-level middleware, except it is bound to an instance of express.Router().
var router = express.Router();
var express = require('express');
var app = express();
var router = express.Router();
// mount the router on the app
app.use('/', router);
// a middleware function with no mount path.
// This code is executed for every request to the router
router.use(function (req, res, next) {
console.log('Time:', Date.now());
next();
});
app.get('/', function(req, res) {
res.send('hello world');
});
app.listen(3000, function() {
console.log('Example app listening on port 3000');
})
// Error-handling middleware always takes
// four arguments (err, req, res, next))
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(500).send('Something broke!');
});
Define error-handling middleware functions in the same way as other middleware functions.
// Error-handling middleware always takes
// four arguments (err, req, res, next))
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(500).send('Something broke!');
});
Define error-handling middleware functions in the same way as other middleware functions.
// Install the Node.js module for the required
// functionality, then load it in your app at
// the application level or at the router level.
var express = require('express');
var app = express();
var cookieParser = require('cookie-parser');
// load the cookie-parsing middleware
app.use(cookieParser());
Use third-party middleware to add functionality to Express apps
Express middleware docs
http://expressjs.com/resources/middleware.html
List of built-in middleware
https://github.com/senchalabs/connect?_ga=1.77493207.337430027.1449182896#middleware
Partial list of third-party middleware