Node js Express and middleware

`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

  1. 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)
  2. 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
`

New contributor

MADHAVI M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật