stream-http-header-parser
v0.0.2
Published
Parse HTTP header in TCP stream
Downloads
10
Readme
stream-http-header-parser
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