express-hello-world Flashcards
How do you mount a middleware with an Express application?
Using the “.use” method on the express application and passing in a path, callback function or both.
Ex: app.use (function (req, res) {
res.send(‘Hello, world!’);
});
Which objects does an Express application pass to your middleware to manage the request/response lifecycle of the server?
The request object (req) and the response object (res).
The request object has information about the request but also has methods for dealing with the request.
The response object handles the response and has methods for handling the response such as attaching information to the response and the res.send method, which cuts off the header and sends the body. The parameter passed into the res.send method can be a Buffer object, a string, an object, Boolean or an Array.
- The middleware function is also usually passed in a “next” middleware function commonly denoted by a variable named “next” to pass control to the next middleware function, if the request-response cycle isn’t ended.