Socket
Socket
Sign inDemoInstall

on-headers

Package Overview
Dependencies
0
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

on-headers

Execute a listener when a response is about to write headers


Version published
Maintainers
1
Weekly downloads
20,041,151
decreased by-8.24%

Weekly downloads

Package description

What is on-headers?

The on-headers npm package is used to execute custom logic when headers are about to be written to the response object in a Node.js HTTP server. This can be useful for logging, setting default headers, or performing other actions based on the headers before they are sent to the client.

What are on-headers's main functionalities?

Execute custom logic before headers are sent

This code sample demonstrates how to use the on-headers package to execute a function right before the headers are sent in an HTTP response. The function `onHeadersListener` is called just before the headers are written to the response.

const onHeaders = require('on-headers');

function onHeadersListener(res) {
  console.log('Headers will be sent soon.');
  // Perform any custom logic here
}

http.createServer(function (req, res) {
  onHeaders(res, onHeadersListener);
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World!');
});

Other packages similar to on-headers

Readme

Source

on-headers

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

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

Keywords

FAQs

Last updated on 22 Feb 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc