Middleware for responses
See original GitHub issueSimilar to how express works there doesn’t seem to be an easy way for an itty middleware to trigger after the response. For express there’s the https://www.npmjs.com/package/express-mung library to mitigate this, but it’s not the most pretty solution. It would be nice if a middleware could be passed a next function that you could await that could return the response object, so something like this:
async function middleware(request, next) {
const response = await next();
console.log(response.status);
}
This would make it possible to do things like error handler middlewares, logging response and keeping track of response times.
Issue Analytics
- State:
- Created 2 years ago
- Comments:14 (3 by maintainers)
Top Results From Across the Web
Writing middleware for use in Express apps
Middleware functions are functions that have access to the request object ( req ), the response object ( res ), and the next...
Read more >Intercepting RESTful Responses with Middleware
An article discussing the application of HTTP middleware to control server responses that would otherwise be influenced by third-party APIs.
Read more >ASP.NET Core Middleware | Microsoft Learn
Middleware is software that's assembled into an app pipeline to handle requests and responses. Each component:.
Read more >Middleware - FastAPI
A "middleware" is a function that works with every request before it is processed by any specific path operation. And also with every...
Read more >Using Middleware in .NET 5.0 to Log Requests and Responses
Imagine a "pipeline" connecting the client to the server. Middleware sits in that pipe, between client and server, and has the ability to ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I mean, by its definition, middleware is simply anything that operates in the middle (in this case of a request and the eventual response/return). Any handler is technically middleware, the same as in Express. The only things differentiating itty’s middleware from Express middleware is:
next()
call to continue to the next handler, vs itty that simply continues the handler chain until something returns. Both have pros/cons. I chose the itty flow because I opt for brevity/simplicity over explicit calls, and the end code gets to look much cleaner as a result. It makes it slightly more difficult to incrementally “assemble” a response, as a consequence.Thanks for the reply!
The use case I have right now is that I would like to log the responses to a S3 firehose, which I think should work fine with doing a then/catch on the handler like you showed above. It would be nifty to be able to use middlewares like in koa-router, but for the time being it’s not something I need.