Execute a listener when a response is about to write headers.
JavaScript
163
204 commits
updated Jun 24, 2026
Execute a listener when a response is about to write headers.
This is a Node.js module available through the
npm registry. Installation is done using the
npm install command:
$ npm install on-headers
var onHeaders = require('on-headers')
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.
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!')
}
$ npm test
Overall
Maintained
Code review
npm
Latest release
Releases this year
479 followers · starred Aug 2014
1,829 followers · starred Aug 2014
32 followers · starred Apr 2026
89 followers · starred Feb 2016
Execute a listener when a response is about to write headers.
JavaScript
163
204 commits
updated Jun 24, 2026
Execute a listener when a response is about to write headers.
This is a Node.js module available through the
npm registry. Installation is done using the
npm install command:
$ npm install on-headers
var onHeaders = require('on-headers')
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.
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!')
}
$ npm test
Overall
Maintained
Code review
npm
Latest release
Releases this year
479 followers · starred Aug 2014
1,829 followers · starred Aug 2014
32 followers · starred Apr 2026
89 followers · starred Feb 2016