Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

servertime

3.1.1 • Public • Published

Add 'server-timing' headers to your node.js server.

If you're wondering why this would be useful, go check out this article.

There are other libraries out there that do this, but this one is the best. ;)

Features:

  • Sub-millisecond accuracy with process.hrtime(), but falls back to millisecond accuracy if you're on some weird platform that doesn't have process.hrtime().
  • No server-timing headers by default on prod.
  • No dependencies on Express, but provides some handy middleware helpers if you're on Express.

Documentation

See the api docs here.

Examples:

import * as servertime from 'servertime';
 
/* -- Use in express app -- */
const app = express();
app.use(servertime.middleware({devOnly: true}));
 
// Time some middlewares
app.use(servertime.start('prerouting'));
app.use(sessionMiddleware);
app.use(otherMiddleware);
app.use(servertime.stop('prerouting');
 
// Time a single middleware
app.use(servertime.timeMiddleware('routing', router));
 
/* -- Use in non-express app -- */
http.createServer((req, res) => {
    servertime.addToResponse(res, {devOnly: true});
 
    // Time something
    res.serverTiming.start('blah');
    // do some stuff
    res.serverTiming.end('blah');
 
    // Time a promise
    let pUser = getUserFromDb();
    pUser = res.serverTiming.timePromise('getuser', 'Get User', pUser);
    pUser.then(user => {
        // Header will be added automatically
        res.end('Done');
    });
});
 
 
/* -- Use standalone -- */
const serverTiming = servertime.createTimer();
serverTiming.start('blah', 'Timing of Blah');
// ...
serverTiming.end('blah');
 
const header = serverTiming.getHeader();

Install

npm i servertime

DownloadsWeekly Downloads

319

Version

3.1.1

License

MIT

Unpacked Size

33.4 kB

Total Files

11

Last publish

Collaborators

  • avatar