Implementing an Express middleware function to be called after route responses

February 06, 2019

This is needed in cases where you're looking to collect some data after a request was processed, whether it's simple logging, or collecting analytics or any other type of action.

After response callback middleware for Express:

app.use((req, res, next) => {
    res.on("finish", () => {
        console.log("After response")
    })

    next()
})