xprezzo-on-headers

1.1.3 • Public • Published

xprezzo-on-headers

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 xprezzo-on-headers

API

var onHeaders = require('xprezzo-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('xprezzo-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', 'Xprezzo')
  }
}

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

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

Testing

$ npm test

People

Xprezzo and related projects are maintained by Cloudgen Wong.

License

MIT

Package Sidebar

Install

npm i xprezzo-on-headers

Weekly Downloads

3

Version

1.1.3

License

MIT

Unpacked Size

7.16 kB

Total Files

5

Last publish

Collaborators

  • xprezzo