1. on-headers
Execute a listener when a response is about to write headers
on-headers
Package: on-headers
Created by: jshttp
Last modified: Mon, 26 Feb 2024 19:49:07 GMT
Version: 1.0.2
License: MIT
Downloads: 85,979,534
Repository: https://github.com/jshttp/on-headers

Install

npm install on-headers
yarn add on-headers

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

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code