3.1.1 • Published 4 years ago

servertime v3.1.1

Weekly downloads
569
License
MIT
Repository
github
Last release
4 years ago

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();
3.1.1

4 years ago

3.1.0

4 years ago

3.0.0

5 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago