You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to create a simple project with this library, but I have some trouble. I think the routes registration are not handled correctly. The handler is called twice every time.
Let's take this simple example. By registering a middleware or a route, the behavior will be what I described above .
importHyperExpress,{MiddlewareNext,Request,Response}from'hyper-express';constwebserver=newHyperExpress.Server();constspecificMiddleware1=(request: Request,response: Response,next: MiddlewareNext)=>{// This is called twiceconsole.log('route specific middleware 1 ran!');next();};constspecific_middleware2=(request: Request,response: Response,next: MiddlewareNext)=>{// This is called twiceconsole.log('route specific middleware 2 ran!');next();};// webserver.use(specificMiddleware1);// webserver.use(specific_middleware2);webserver.get('/',// {// middlewares: [specificMiddleware1, specific_middleware2],// },(request: Request,response: Response)=>{// This is called twiceconsole.log('called');response.send('Hello World');},);webserver.listen(3000);
`
The text was updated successfully, but these errors were encountered:
Hello, It seems that you misunderstood how the middleware binding works. After trying your snippet above, I am seeing the following behavior:
The route handler aka. called log only gets logged once no matter what.
The specific middleware 1 and 2 only run twice If you do webserver.use() AND also specify a middlewares property on the route options. By doing this, you are effectively binding the same middleware twice.
The webserver.use() call is binding a global middleware meaning the middleware being assigned will run on ALL routes on the server.
On the contrary, the middlewares object specified on the route options will only run those middlewares on that route's requests.
The route handler should never be called twice. You can validate that the route handler never gets called by using code like below:
webserver.get('/', (request, response) => {
// The below code will only log the warning if the same request is called twice on the handler
if (request.called_already) console.warn('Detected the same route handler being called twice with the same request');
request.called_already = true;
});
With that said, this does not seem to be a bug in hyper-express but rather incorrect usage. Hope that helps!
Hi,
I'm trying to create a simple project with this library, but I have some trouble. I think the routes registration are not handled correctly. The handler is called twice every time.
Let's take this simple example. By registering a middleware or a route, the behavior will be what I described above .
The text was updated successfully, but these errors were encountered: