In the route app.get('/now', ...) chain a middleware function and the final handler. In the middleware function you should add the current time to the request object in the req.time key. You can use new Date().toString(). In the handler, respond with a JSON object, taking the structure {time: req.time}.
Question
In the route app.get('/now', ...) chain a middleware function and the final handler. In the middleware function you should add the current time to the request object in the req.time key. You can use new Date().toString(). In the handler, respond with a JSON object, taking the structure {time: req.time}.
Solution
Here is a step-by-step guide on how to chain a middleware function and the final handler in Express.js:
- First, you need to define your middleware function. This function will add the current time to the request object in the
req.timekey. Here's how you can do it:
let addTime = (req, res, next) => {
req.time = new Date().toString();
next();
};
- Next, you need to chain this middleware function to your route. You can do this by passing the middleware function as a second argument to the
app.get()function, before the final handler function:
app.get('/now', addTime, (req, res) => {
// final handler code here
});
- In the final handler, you need to respond with a JSON object that includes the time from the
req.timekey. You can do this using theres.json()function:
app.get('/now', addTime, (req, res) => {
res.json({time: req.time});
});
So, the complete code would look like this:
let addTime = (req, res, next) => {
req.time = new Date().toString();
next();
};
app.get('/now', addTime, (req, res) => {
res.json({time: req.time});
});
This code will add the current time to the request object in the middleware function, then respond with a JSON object that includes this time in the final handler.
Similar Questions
Which method is used to define a route that handles GET requests in Express?app.get()app.fetch()app.retrieve()app.query()
Build an API endpoint, mounted at GET /name. Respond with a JSON document, taking the structure { name: 'firstname lastname'}. The first and last name parameters should be encoded in a query string e.g. ?first=firstname&last=lastname.Note: In the following exercise you are going to receive data from a POST request, at the same /name route path. If you want, you can use the method app.route(path).get(handler).post(handler). This syntax allows you to chain different verb handlers on the same path route. You can save a bit of typing, and have cleaner code.
How is it possible to create chainable route handlers for a route path in Express.js?(1 Point)Using app.router()Using app.routing()Using app.route()Using app.routes()
Which express.js feature allows you to define routes based on HTTP methods and URLs?MiddlewareTemplatingRoutingDebugging
How do you handle POST requests in Express.js? a. Using the express.post() method b. Using the app.route() method c. Using the app.post() method d. Using the app.get() method
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.