How to use the on-headers function in on-headers

To help you get started, we’ve selected a few on-headers examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github charjac / nest-boilerplate / src / common / logger / logger.middleware.ts View on Github external
return (req, res, next) => {
      let jsMeter: JsMeter;
      if (this.env.DEBUG) {
        jsMeter = new JsMeter({ isPrint: false, isMs: true, isKb: true });
      }
      const startAt = Date.now();

      onHeaders(res, () => {
        const diff = Date.now();
        const time = diff - startAt;
        this.logger.info(
          `${req.method} ${req.originalUrl} ${res.statusCode} +${time}ms`,
        );
        if (this.env.DEBUG) {
          const {
            diffCPU,
            diffHeapTotal,
            diffHeapUsed,
            diffExternal,
            diffRAM,
          } = jsMeter.stop();
          this.logger.debug(
            `CPU: ${diffCPU}ms RAM: ${diffRAM}kb HeapTotal: ${diffHeapTotal}kb HeapUsed: ${diffHeapUsed}kb External: ${diffExternal}kb`,
          );
github EvaEngine / EvaEngine.js / src / middlewares / trace.js View on Github external
statusCode: null,
      queries: [],
      debug: {}
    };

    res.set({
      'X-Service-Name': serviceName,
      'X-Requested-At': timestamp,
      'X-B3-SpanId': spanId,
      'X-B3-TraceId': traceId,
      'X-B3-ParentSpanId': parentId,
      'X-B3-Sampled': enabled ? 1 : 0
    });

    if (sampled < 1) {
      onHeaders(res, () => {
        const [seconds, nanoseconds] = process.hrtime(startedAt);
        const duration = ((seconds * 1e3) + (nanoseconds * 1e-6));
        res.set('X-Response-Milliseconds', parseInt(duration, 10));
      });
      next();
      return;
    }

    onHeaders(res, () => {
      const [seconds, nanoseconds] = process.hrtime(startedAt);
      const duration = ((seconds * 1e3) + (nanoseconds * 1e-6)) * 1000;
      tracer.duration = parseInt(duration, 10);
      tracer.statusCode = res.statusCode;
      res.set('X-Response-Milliseconds', tracer.duration / 1000);

      const useHeader = config.get('trace.header');
github nuxt / nuxt.js / packages / server / src / middleware / timing.js View on Github external
export default options => (req, res, next) => {
  if (res.timing) {
    consola.warn('server-timing is already registered.')
  }
  res.timing = new ServerTiming()

  if (options && options.total) {
    res.timing.start('total', 'Nuxt Server Time')
  }

  onHeaders(res, () => {
    res.timing.end('total')

    if (res.timing.headers.length > 0) {
      res.setHeader(
        'Server-Timing',
        []
          .concat(res.getHeader('Server-Timing') || [])
          .concat(res.timing.headers)
          .join(', ')
      )
    }
    res.timing.clear()
  })

  next()
}

on-headers

Execute a listener when a response is about to write headers

MIT
Latest version published 6 years ago

Package Health Score

67 / 100
Full package analysis

Popular on-headers functions