-
-
Notifications
You must be signed in to change notification settings - Fork 16.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Second middleware stack #2255
Comments
this is way too complicated and would require rewriting all the middleware. at this point, you might as well use another framework like koa that doesn't have this issue |
Another way to handle it could be through emitting more events. Hapi does something like this, allowing you to write hooks for postResponse for example. |
The other alternative is to allow a middleware to replace req and res, letting them be made into transform streams or other interposed constructs. |
part of the beauty of express middleware (at least the ones we provide) is that you don't need express to use these middleware. you can create a pyramid of with both suggestions, you're creating a much more opinionated framework with less compatibility trying to solve a problem that shouldn't exist in the first place. the best solution is just to get rid of the problem: callbacks. i'm also -1 on streams because streams are super broken and it's non-trivial to create a well-written stream utility/middleware. |
replacing |
I am also generally -1 on this (especially since the example above is trivially implemented by a shared function), but am willing to hear a compelling argument for the added complexity. |
Although the interface for middleware is common I think it's too general an assumption to say all middleware is interoperable. In any case, the intent of this thread, unless I am mistaken, is to call for control over the response pipeline in a fashion similar to that of middleware. Personally I think this is a feature lacking in express / connect that is not simply an edge case and could benefit the community greatly. |
Node.js core events do not stack, so it would not work for the use-case in the OP.
This will lead to issues caused by things like
We are specifically talking about the middleware we provide. They don't even use express/connect in their tests. The current pattern is completely interoperable, though it doesn't mean people have to make their middleware that way (read: they are lazy). |
Replacing
|
|
Yeah, or delegate to the original. Certainly not the most elegant thing in the universe, but not a collossal hack. |
The following is the public interface of
I'm sure I'm missing things. This also does not include all the possible signatures to the methods. |
Also, @aredridel I'm going to extract a utility as a module for the community where you can give a |
The module also forwards back-pressure, etc. |
That'd do the job. Ugly in the same ways, inside-out. Clever, too. |
@aredridel you're welcome you subscribe to expressjs/discussions#301 to know when/were it lands. |
That would be awesome. |
I don't think this is something we should be pushing for right now. |
Actually, thinking about it... We do have second middleware stack. That's error-handling middlewares. Are there any side-effects for re-purposing that one for general usage, not just for errors? I mean, the code above could be rewritten like this: app.get('/', function(req, res, next) {
next({hello: 'world'}) // this would be response, not an error
})
app.use(function(body, req, res, next) {
if (body instanceof Error) return next(body)
next(YAML.stringify(body))
})
app.use(function(body, req, res, next) {
if (typeof(body) === 'string') {
res.header('content-encoding', 'gzip')
body = gzip(body)
}
next(body)
}) ... and the funny thing is that it's already working perfectly fine. I wonder why I didn't notice that before. :D |
Just sharing my thoughts about
res.send()
mentioned in #2249 (comment)This is just an idea, and it has a lot of flaws as is, but maybe somebody would find it useful.
So, right now
res.send()
terminates middleware stack.next()
handler is not called, so no middlewares won't be executed. Well, except for those who monkey-patchesres.send()
of course, but people usually see it as a hack.Currently all middlewares are supposed to be executed before result of the request is known.
Here is a typical express application with a few middlewares.
Please note that etagify, compress and that weird custom formatter are usually only overriding res.send() and their main work is done after res.send is executed.
And because of the way how they work, etagify is specified before compress, but it is actually called after it.
Idea:
res.send(data)
could pass the data to a second middleware stack. This stack would know that the result of the request is ready and perform some operations over it.Which some people might find more clear.
Note that real-world middlewares are much more complex because of the streaming, and I don't know how to deal with this yet. Well, maybe do
res.send(Stream)
and make use of transform streams.The text was updated successfully, but these errors were encountered: