createRequestListener

import { createRequestListener } from '@hullo/node'

This creates a function you can provide to http.createServer or https.createServer.

Both request body and response body are streams of string | ArrayBuffer.

Following example creates a HTTP server on each request and returns a response that consists of a request method, request path and request body - a simple echo service.

import * as http from "http";
import { of, flatMap } from "@hullo/core";
import { createRequestListener } from "@hullo/node";
http
.createServer(
createRequestListener(async request => ({
status: 200,
headers: {},
body: of([
of<ArrayBuffer | string>(`ECHO\n`),
of<ArrayBuffer | string>(`Method: ${request.method}\n`),
of<ArrayBuffer | string>(`Path: ${request.url}\n`),
request.raw
]).pipe(flatMap(v => v))
}))
)
.listen(81);

And the response will look like:

> curl -F 'data=DATA_DATA_DATA_DATA' http://localhost:81
ECHO
Method: POST
Path: /
--------------------------c631848d1807aa8f
Content-Disposition: form-data; name="data"
DATA_DATA_DATA_DATA
--------------------------c631848d1807aa8f--

To fully read the example see also: