Sha256: e19e429e8fb41228da43769966c6c89aa2450a28bf38ccc0f081d47469ef65a0

Contents?: true

Size: 1.58 KB

Versions: 17

Compression:

Stored size: 1.58 KB

Contents

# on-headers

[![NPM Version](https://img.shields.io/npm/v/on-headers.svg?style=flat)](https://www.npmjs.org/package/on-headers)
[![Node.js Version](https://img.shields.io/badge/node.js->=_0.8-blue.svg?style=flat)](http://nodejs.org/download/)
[![Build Status](https://img.shields.io/travis/jshttp/on-headers.svg?style=flat)](https://travis-ci.org/jshttp/on-headers)
[![Coverage Status](https://img.shields.io/coveralls/jshttp/on-headers.svg?style=flat)](https://coveralls.io/r/jshttp/on-headers)
[![Gittip](https://img.shields.io/gittip/dougwilson.svg?style=flat)](https://www.gittip.com/dougwilson/)

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

## Install

```sh
$ npm install on-headers
```

## API

```js
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 `listener`s are fired
in the reverse order they were added.

## Examples

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

http
.createServer(onRequest)
.listen(3000)

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

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

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

## License

[MIT](LICENSE)

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
hooch-0.4.2 jasmine/node_modules/karma/node_modules/connect/node_modules/on-headers/README.md
hooch-0.4.1 jasmine/node_modules/karma/node_modules/connect/node_modules/on-headers/README.md
hooch-0.4.0 jasmine/node_modules/karma/node_modules/connect/node_modules/on-headers/README.md
hooch-0.3.0 jasmine/node_modules/karma/node_modules/connect/node_modules/on-headers/README.md
hooch-0.2.1 jasmine/node_modules/karma/node_modules/connect/node_modules/on-headers/README.md
hooch-0.2.0 jasmine/node_modules/karma/node_modules/connect/node_modules/on-headers/README.md
hooch-0.1.0 jasmine/node_modules/karma/node_modules/connect/node_modules/on-headers/README.md
hooch-0.0.8 jasmine/node_modules/karma/node_modules/connect/node_modules/on-headers/README.md
hooch-0.0.7 jasmine/node_modules/karma/node_modules/connect/node_modules/on-headers/README.md
hooch-0.0.6 jasmine/node_modules/karma/node_modules/connect/node_modules/on-headers/README.md
entangled-0.0.16 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/on-headers/README.md
entangled-0.0.15 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/on-headers/README.md
entangled-0.0.14 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/on-headers/README.md
entangled-0.0.13 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/on-headers/README.md
entangled-0.0.12 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/on-headers/README.md
entangled-0.0.11 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/on-headers/README.md
entangled-0.0.10 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/on-headers/README.md