Express REST API

1. Name 3 real world use cases where you’d want to change the request with custom middleware
-
In one scenario you can identify the currently logged in user, so using the custom middleware fetches the user through a set of authentication steps.
-
A second example is changing the functionality on your response object, such as creating a new header. I could foresee a company doing that to advertise a sale of a product.
-
Building on the fact that a user logs in, the custom middleware can be uses to also validate a users data. The users information and credentials could be stored in a database to verify the information is correct.
in other words :
-
Application level middleware
-
Built in middleware
-
Error handling middleware
2. True or false: The route handler is middleware?
True, route handling is a part of middleware functionality. The exception is route handling may invoke the next function parameter to pass control onto the middleware function within the stack.
3. In what ways can a middleware function end the process and send data to the browser?
-
res.send()
-
res.redirect()
-
res.render()
-
res.sendFile()
-
res.download()
4. At what point in the request lifecycle can you “inject” middleware?
when sending a request or a request parameter by the client side will inject the middleware so Middleware is usually injected during a req and before a response cycle, this means that middleware is a dependency and the req and res cycle depend on the middleware to take place.
5. What can cause express to error with “Request headers sent twice, cannot start a second response”
means that you’re already in the Body or Finished state, but some function tried to set a header, like callbacks invoked twice for example.
Document the following Vocabulary Terms
Middleware: functions that have access to the request object (req), the response object (res), that can Execute any code. Make changes to the request and the response objects.
