on-headers (1.1.0)

Published 2026-02-24 14:02:08 +00:00 by atheaadmin

Installation

registry=
npm install on-headers@1.1.0
"on-headers": "1.1.0"

About this package

on-headers

NPM Version NPM Downloads Node.js Version Build Status Coverage Status

Execute a listener when a response is about to write headers.

Installation

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install on-headers

API

var onHeaders = require('on-headers')

onHeaders(res, listener)

This will add the listener listener to fire when headers are emitted for res. The listener is passed the response object as it's context (this). Headers are considered to be emitted only once, right before they are sent to the client.

When this is called multiple times on the same res, the listeners are fired in the reverse order they were added.

Examples

var http = require('http')
var onHeaders = require('on-headers')

http
  .createServer(onRequest)
  .listen(3000)

function addPoweredBy () {
  // set if not set by end of request
  if (!this.getHeader('X-Powered-By')) {
    this.setHeader('X-Powered-By', 'Node.js')
  }
}

function onRequest (req, res) {
  onHeaders(res, addPoweredBy)

  res.setHeader('Content-Type', 'text/plain')
  res.end('hello!')
}

Testing

$ npm test

License

MIT

Dependencies

Development Dependencies

ID Version
eslint 6.8.0
eslint-config-standard 14.1.1
eslint-plugin-import 2.21.2
eslint-plugin-markdown 1.0.2
eslint-plugin-node 11.1.0
eslint-plugin-promise 4.2.1
eslint-plugin-standard 4.0.1
mocha 10.2.0
nyc 15.1.0
supertest 4.0.2

Keywords

event headers http onheaders
Details
npm
2026-02-24 14:02:08 +00:00
0
Douglas Christopher Wilson
MIT
latest
3.5 KiB
Assets (1)
Versions (1) View all
1.1.0 2026-02-24