DevLeague Coding Bootcamp
DevLeague is a Full Stack Coding Bootcamp
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:
const express = require('express');
const app = express();
app.get('/', (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.
const app = express();
app.use((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', (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', (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().
const router = express.Router();
const express = require('express');
const app = express();
const 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((req, res, next) => {
console.log('Time:', Date.now());
next();
});
app.get('/', (req, res) => {
res.send('hello world');
});
app.listen(3000, () => {
console.log('Example app listening on port 3000');
});
// Error-handling middleware always takes
// four arguments (err, req, res, next))
app.use((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((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.
const express = require('express');
const app = express();
const 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
By DevLeague Coding Bootcamp
The heart of Express