stream-http-header-parser

Parse HTTP header in TCP stream

Usage no npm install needed!

<script type="module">
  import streamHttpHeaderParser from 'https://cdn.skypack.dev/stream-http-header-parser';
</script>

README

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