Closed younanjo closed 2 years ago
I haven't figured out a way to do this, either, but would be interested in this functionality. in Gin for Golang any code run after calling next
in a middleware is run after next has completed. A solution might be to do the same here. I.e.
nc<NextApiRequest, NextApiResponse>({})
.use(async (req, res, next) => {
console.log("I'm run before the main GET function");
next();
console.log("I'm run after the main GET function");
})
.get((req, res) => {
res.status(200).json({ yes: "Yes!" });
});
This seems to model the flow like in koa
, in which case, is not currently supported because next-connect
models after connect
/express
. There is a PR to address this pattern https://github.com/hoangvvo/next-connect/pull/148. I will give it some thought in the next several days whether we should switch over to that (unlikely because of breakability) or add it as an "extra".
@hoangvvo any thoughts on supporting such a feature? If not, would you be able to suggest a pattern on how one would about logging req/res ?
@hoangvvo any thoughts on supporting such a feature? If not, would you be able to suggest a pattern on how one would about logging req/res ?
Did you try the morgan middleware? https://github.com/expressjs/morgan
does anyone know if it's possible to have a handler that always runs at the end of the chain? A use-case may be a logger middleware that logs to stdout once all middleware have run. For example if you've got:
How do you add
logger
so that it logs every req/req as part of the middleware chain but ran last? I don't want to make everyget/post/put...
responsible for its own logging. How are you peeps doing it?