-
Notifications
You must be signed in to change notification settings - Fork 102
Unable to execute server-level middleware after response is sent using InversifyExpressServer #425
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
Comments
Hey @madmatah, const statusCodeLoggerMiddleware = (req, res, next) => {
console.log(`STATUS: ${res.statusCode}`);
next();
};
@controller('/test')
@withMiddleware(statusCodeLoggerMiddleware)
export class TestController extends BaseHttpController {
@httpGet('/success') public getSuccess() {
return 'OK';
}
@httpGet('/teapot') public getTeapot() {
return this.statusCode(418);
}
}
const container = new Container();
const server = new InversifyExpressServer(container);
const app = server.build(); I'll close the issue, but we can keep discussing it if you have any doubt. |
Hello @notaphplover and thank you for your reply! The example that you gave using And that's because the middleware is registered for execution before my controller code. The main point of this issue is that I want to be able to run middleware after the response has been sent. I've taken a simple example here to illustrate my problem, but a more realistic example would be to create middleware that collects statistics on the response time of all my routes. I can do it easily with express, but I'm stuck to do it with InversifyExpressServer. And the second point is that I'm looking for a way to declare this kind of middleware on a server-level, to execute it everytime. I don't want to decorate all my controllers for this use case. The |
Hey @madmatah , I see. being totally honest I thought when a middleware ends the request response cycle, the next middleware should not be invoked, but after having a look at the docs it's not forbidden. I'm reopening the issue now |
Hi @madmatah this worked for me.
|
@notaphplover Thanks! Indeed, Express is quite permissive and both ways of doing things are valid. In order not to break anything, how about making this behaviour configurable by adding an option to the InversifyExpressServer constructor? (e.g. |
@santosh-sambhavani Hello and thanks for your response. This example does not work as expected: the middleware is executed before the controller. If I start the server and make a
The expected result is |
Uh oh!
There was an error while loading. Please reload this page.
Is there an existing issue for this?
Current behavior
I would like to run some server-level middlewares (that are not error handlers) after the HTTP response has been sent.
For example, I want to log all status codes returned by my server:
I used
setErrorConfig()
method to register my middleware after the controller routes.But it is never executed.
Did I miss something to get the expected result?
Steps to reproduce
No response
Expected behavior
I expect middlewares registered after the controller routes to be executed.
In the current state, only error handlers are executed.
Possible solution
I think the handlerFactory() method in
InversifyExpressServer
should callnext()
after the response is sent:I can make a PR if this solution is OK for you.
Package version
6.2.1
Node.js version
20.11.1
In which operating systems have you tested?
Stack trace
No response
Other
inversify-express-utils
version6.4.10
The text was updated successfully, but these errors were encountered: