on-headers
Execute a listener when a response is about to write headers
Last updated 9 months ago by ulisesgascon .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install on-headers 
SYNC missed versions from official npm registry.

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

Current Tags

  • 1.1.0                                ...           latest (9 months ago)

5 Versions

  • 1.1.0                                ...           9 months ago
  • 1.0.2                                ...           7 years ago
  • 1.0.1                                ...           11 years ago
  • 1.0.0                                ...           12 years ago
  • 0.0.0                                ...           12 years ago
Downloads
Today 0
This Week 71
This Month 99
Last Day 11
Last Week 42
Last Month 186
Dependencies (0)
None

Copyright 2013 - present © cnpmjs.org | Home |