stream-http-header-parser

0.0.2 • Public • Published

stream-http-header-parser Build Status

Parse HTTP header in TCP stream

Install

$ npm install --save stream-http-header-parser

Usage

const net = require('net');
const createHeaderParser = require('stream-http-header-parser');
 
net.createServer(createHeaderParser((headers, stream) => {
  if (headers.method === 'GET') {
    stream.on('data', chunk => {
      const data = chunk.toString();
      stream.end(`HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: ${data.length}\r\n\r\n${data}`);
    });
  } else {
    stream.end(`HTTP/1.1 404 Not Found`);
  }
})).listen(3000);

API

createHeaderParser(callback)

Returns a Function that should be called with a stream.Duplex (e.g. net.Socket) that you want to parse.

callback(headers, stream)

Type: Function

The callback will be called after HTTP headers are parsed. headers is an object with the actual headers (see http-headers for details). stream is a stream.Duplex and it's basically a wrapper around the original stream. Note that the stream will output all the data from the original stream, including the headers.

Dependencies

License

MIT © Michał Nykiel

Readme

Keywords

none

Package Sidebar

Install

npm i stream-http-header-parser

Weekly Downloads

0

Version

0.0.2

License

MIT

Last publish

Collaborators

  • marszall87