DEV Community

Aman Pareek
Aman Pareek

Posted on

Execute function after response sent to client - express nodejs

 app.use((req, res, next) => {
    const originalEnd = res.end;

    if (req.method !== "GET" && res.statusCode < 400) {
      res.end = function (chunk, encoding) {
        originalEnd.call(this, chunk, encoding);

        mainBuilder(); // function i want to execute
      };
    }
    next();
  });

Enter fullscreen mode Exit fullscreen mode

Explanation youtube video

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more