Skip to content
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

Express Connect Middleware does not call next() for handled requests #551

Closed
easyCZ opened this issue Mar 29, 2023 · 4 comments
Closed

Express Connect Middleware does not call next() for handled requests #551

easyCZ opened this issue Mar 29, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@easyCZ
Copy link

easyCZ commented Mar 29, 2023

Describe the bug

In the implementation of the express connect middleware, the middleware only calls next() when it does not find a corresponding registered connect handler.

However, it should also call next() after it finds an appropriate handler, and handles the request. This then allows any registered middlewares afterwards to also run. This is useful for any number of use-cases, such as logging, telemetry, etc and in the absence of

it is the only way to actually get some telemetry out after the request has been handled.

To Reproduce

app.use(
      (req, res, next) => {
          	console.log("pre middleware");
			next();
      },
      expressConnectMiddleware({ routes }),
      (req, res, next) => {
          	log.info("post middleware");
			// this is never executed
      },
  );

If you encountered an error message, please copy and paste it verbatim.
If the bug is specific to an RPC or payload, please provide a reduced
example.

Environment (please complete the following information):

  • @bufbuild/connect-web version: latest
  • @bufbuild/connect-node version: latest
@easyCZ easyCZ added the bug Something isn't working label Mar 29, 2023
@timostamm
Copy link
Member

Thanks for the issue, Milan, we'll get this fixed.

@easyCZ
Copy link
Author

easyCZ commented Mar 30, 2023

@timostamm I can try to put in a PR, if you'd like. I am, however, not that proficient with express to judge if what I'm asking is indeed the correct pattern to go for.

@smaye81
Copy link
Member

smaye81 commented Apr 3, 2023

Hi @easyCZ, in looking at this and Express documentation, I think our middleware is behaving according to Express semantics. If you look at the docs here (4th section), it says:

If the current middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function. Otherwise, the request will be left hanging.

Our middleware does end the request-response cycle (unless a route isn't found for a specific request) so I think it's working correctly here. I understand what you're asking though. What about using a basic middleware function in addition to ours that added an event listener to the close event of the response? So something like:

app.use((req, res, next) => {
    console.log("got request", req.url);
    res.on("close", () => {
        console.log("emit metrics here", req.url);
    });
    next();
})

@smaye81
Copy link
Member

smaye81 commented Apr 10, 2023

@easyCZ going to close this issue but feel free to re-open or create a new one if you notice something still not working correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants