`Can someone help me with the below question as i’m unable to pass test requirements
Node js:ExpressJs Middileware Basics
A company is launching a task manager app soon.The team has already created an expressjs app with several routes to create and manage tasks.Now they will extend the default request object in ExpressJs and attach a utility on top to perform smart tracing of the system.Create an ExpressJs middleware that attaches a trace object to each request in the system
the middleware should be stacked to each request just before the route handler for each of the routes.write the middleware in the file middleware.js and make it the default export of the file .Also, mount the middleware function into the Express stack just before the route handlers only for /tasks routes. it should not be available for any other routes
The trace object should contain the following properties
1.id(property) – A unique that identifies the request.[STRING]
2.timestamp(property)- the timestamp when the request was sent in UTC(GMT+0).[EPOCH INTEGER]
3.path(property)- the path of the request without the hostname,for example /path.[STRING]
Example
If the request is made to http://localhost:8000/path ,the trace object should be
{
“id”:”Zx43494mdAds”,
“timestamp”:1585509746462,
“path”:”/path”
}
After the trace object has been mounted on the request object,set the ID property in the response header.The nae of the header must be x-request-id.Then call the next function of the middleware so that the control can be passed to the next handler function
Test Requirements
- The middleware function should be written in the file middleware.js and the function should be the default export of the file.
2.The middleware should be added just before the route handlers for the /tasks end points and should not be available for ‘/’ (root) - the function must call the next() function so that the control is passed to the route handlers once the object is attached to the request object and the header request-id has been added to the request object
`
middleware.js
const { v4: uuidv4 } = require('uuid');
const traceMiddleware = (req, res, next) => {
const trace = {
id: uuidv4(),
timestamp: Math.floor(Date.now() / 1000), // EPOCH time in seconds
path: req.originalUrl
};
req.trace = trace;
res.setHeader('x-request-id', trace.id);
next();
---
module.exports = traceMiddleware; // default export
app.js
const express = require('express');
const app = express();
const traceMiddleware = require('./middleware');
const tasksRouter = express.Router();
tasksRouter.use(traceMiddleware); // Apply the middleware to all /tasks routes
tasksRouter.get('/', (req, res) => {
res.send('List of tasks');
});
tasksRouter.post('/', (req, res) => {
res.send('Task created');
});
// Use the tasksRouter for /tasks routes
app.use('/tasks', tasksRouter);
// Other routes
app.get('/', (req, res) => {
res.send('Welcome to the Task Manager App');
});
const PORT = process.env.PORT || 8000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
> i need to pass all the test cases
`
MADHAVI M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.