JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 413746
  • Score
    100M100P100Q223041F
  • License MIT

Parse http headers

Package Exports

  • http-headers

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (http-headers) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

http-headers

Build status js-standard-style

Extract and parse headers from an HTTP request or reponse.

Converts:

HTTP/1.1 200 OK
Date: Tue, 10 Jun 2014 07:19:27 GMT
Connection: keep-alive
Transfer-Encoding: chunked

Hello World

To this:

{ date: 'Tue, 10 Jun 2014 07:19:27 GMT',
  connection: 'keep-alive',
  'transfer-encoding': 'chunked' }

Features:

  • Auto-detects and ignores HTTP start-line if present
  • Auto-detects and ignores body if present
  • Fully RFC 2068 compliant (please open an issue if you find a discrepancy)
  • Support multi-line headers (lines will be joined with a space)
  • Support repeating headers (values will be joined with , )

Installation

npm install http-headers

Usage

var net = require('net')
var httpHeaders = require('http-headers')

// create TCP server
net.createServer(function (c) {
  var buffers = []
  c.on('data', buffers.push.bind(buffers))
  c.on('end', function () {
    var data = Buffer.concat(buffers)

    // parse incoming data as an HTTP request and extra HTTP headers
    console.log('Request headers:', httpHeaders(data))
  })
}).listen(8080)

http.ServerReponse support

If given an instance of http.ServerResponse, the reponse headers is automatically extracted, parsed and returned:

var http = require('http')
var httpHeaders = require('http-headers')

http.createServer(function (req, res) {
  res.end('Hello World')
  console.log('Response headers:', httpHeaders(res))
}).listen(8080)

Why?

If you've ever needed to log or in another way access the headers sent to the client on a http.ServerResponse in Node.js, you know it's not as easy as with the http.IncomingMessage headers (which you just access via request.headers['content-type']).

Response headers are not directly available on the response object. Instead all headers are preprocessed as a string on the private response._header property and needs to be processed in order to be available as an object.

This module makes the task super simple.

API

The http-headers module exposes a single parser function:

httpHeaders([ string | buffer | http.ServerReponse ])

The module returns a JavaScript object with each element representing a parsed header. All header names are lowercased.

License

MIT