I m unable to understand what do I need to do in next step, In my middleware I am trying to log out a value, is that right? but if that is the case, why is not logging out and what is next?
The middleware handler should set the property value and then call next, the final handler should res the value of the property using the required structure {time: req.time}
I am not sure I follow, from what I understood is that middleware is function that runs and what we do in it, is ask the backend to get us something like @evaristoc said and next means to skip middleware and go to handler? Following is my code can you help me figuring out what I am missing, right now learning BE is too confusing
You have to set req.time in the middleware handler to the date as required.
You have to call next() in the middleware handler so it passes control.
The final handler callback should be provided req,res, that is how it has access to the value set on req in the middleware.
In the final handler, you have to res the value you set in the middleware handler.
app.get(
"/somePath",
(req, res, next) => {
// middleware handler
// set some value
// passes control to the next handler
next();
},
(req, res) => {
// runs after next() is called
// res some value set in middleware
}
);
next hands off control to the next handler.
You can add as many handlers you like. They each need to pass control to the next handler by calling next()
These routing methods specify a callback function (sometimes called “handler functions”) called when the application receives a request to the specified route (endpoint) and HTTP method. In other words, the application “listens” for requests that match the specified route(s) and method(s), and when it detects a match, it calls the specified callback function.
In fact, the routing methods can have more than one callback function as arguments. With multiple callback functions, it is important to provide next as an argument to the callback function and then call next() within the body of the function to hand off control to the next callback.
More middleware syntax examples can be found in the docs: