Lecture and Reading: Express - sarahduv-401-advanced-javascript/seattle-javascript-401d32 GitHub Wiki
Express
Middleware
- This is something that has access to not only the request but also the response that needs to be provided. In addition, it will have access to any other middleware needed to complete the request-response cycle.
What do we use middleware for?
- These functions are used to modify the request or response. Such as --> parse the data, return data relating to a specific ID, adding response headers, ect..
Routes
- If you want to restrict the middleware to a specific route, you place the route as the first argument. For example: app.use('/categories', function(request, response, next)).
Example
- 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()
})