HMHasan.dev
Back to Resources
Snippet· 1 min

Express middleware: cheap request timing

Add Server-Timing headers in three lines.

Drop this on your Express app to get per-request timing surfaced in browser devtools — no APM required.

export const timing = (req, res, next) => {
  const t = process.hrtime.bigint();
  res.on("finish", () => {
    const ms = Number(process.hrtime.bigint() - t) / 1e6;
    res.setHeader("Server-Timing", `app;dur=${ms.toFixed(1)}`);
  });
  next();
};