I'm new to Express and Typescript in general and I want to understand how middleware's work in the request-response chain. Below, I have a function which starts the express server and configures two middleware's one after the other.
export async function startExpressServer() {
...
app.use(firstMiddleware);
app.use(secondMiddleware);
...
}
I understand that requests flow through this middleware chain. Each middleware does it's own processing and then calls next() in order to pass execution to the next middleware.
My question is: If I modify/add data to the req/res objects in the second middleware, can I have access to those modifications in the first middleware?
export const firstMiddleware: Handler = (req, res, next) => {
// do some processing here
next(); // call next function to go to secondMiddleware
// can I write some logic here to access the modified req/res objects from the secondMiddleware?
}
export const secondMiddleware: Handler = (req, res, next) => {
// add something to the req/res objects
next();
}
Basically, I'm trying to figure out if the request processing chain is one-way or two-way. If the data flows only in one direction, do you know of an alternative that may come handy in my use case?
Thank you!