Express REST API
Name 3 real world use cases where you’d want to change the request with custom middleware
- handling error and catch all.
- change in request object or response object
- to do something into request object.
True or false: The route handler is middleware?
False
In what ways can a middleware function end the process and send data to the browser?
Using “next()” when all process finished or when faced error using next with error message
At what point in the request lifecycle can you “inject” middleware?
is called before the route handler.
## What can cause express to error with “Request headers sent twice, cannot start a second response” It usually happens when people treat an async response inside an express route as a synchronous response and they end up sending data twice.
| Term | Def |
|---|---|
| Middleware | 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. The next middleware function is commonly denoted by a variable named next. |
| Request Object | The express.js request object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on. |
| Application Middleware | is software that provides common services and capabilities to applications outside of what’s offered by the operating system. Data management, application services, messaging, authentication, and API management are all commonly handled by middleware. |
| Routing Middleware | Middlewares are features added on top of his basic handler, in the form of a stack of functions that take this request into a pipeline doing stuff with it (logging, parsing body, security …ect). Router is one of those middleware, what it does actualy is to take the original request, and forward it to a sub handler according to the path example : “/home” for a GET request is mapped to function getHome that handle it and eventually send a response to the client on the behalf of the original handler. |
| Test Driven Development | it will serve the purpose of exposing any defects present when interactions happen between software modules at the time of their integration. |
| Behavioral Testing | often focused on the behaviour of users rather than the technical functions of the software. To spread out the product ideas, BDT uses very easily understandable language to make sure everyone on the team does not miss anything, even they are non-technical people. |