Project maintained by mr-attaHosted on GitHub Pages — Theme by mattgraham
Readings: Express REST API
Review, Research, and Discussion
Name 3 real world use cases where you’d want to change the request with custom middleware ?
when you want to manipulate the request (like:add current server time to the request)
Add query params
Handling error
True or false: The route handler is middleware?
false, They are not middleware functions by definition
In what ways can a middleware function end the process and send data to the browser?
next(" "); when face somthing bad , will end .
next(); when it finished the process .
At what point in the request lifecycle can you “inject” middleware?
after the request
What can cause express to error with “Request headers sent twice, cannot start a second response”
If you pass anything to the next() function (except the string ‘route’), Express regards the current request as being an error and will skip any remaining non-error handling routing and middleware functions.
When a POST request is sent to /api/route1 it will run every line in the callback. A Can’t set headers after they are sent error message will be thrown because res.json() is called twice, meaning two responses are sent.