Search code examples
node.jsexpressmorgan

Morgan logs the requests after the router is invoked


I expect that Morgan first catch the request and log it (for example GET /home 200) and then, if I've a console log inside the home router, print it.

Router.get("/home", (req, res) => { console.log("router invoked!" });

And this happens on the Terminal:

router invoked! GET /home 200

Shouldn't be printed upside down?


Solution

  • Morgan is designed to log when the response is sent. It does this so that it can include relevant information from the response - for example, the response status code.

    You can turn off this behavior by initializing it with the immediate option set to false (documentation here), but, as mentioned above, you will only be able to log the request and not the response.