Hullo JS
Search…
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.
1
import * as http from "http";
2
import { of, flatMap } from "@hullo/core";
3
import { createRequestListener } from "@hullo/node";
4
​
5
http
6
.createServer(
7
createRequestListener(async request => ({
8
status: 200,
9
headers: {},
10
body: of([
11
of<ArrayBuffer | string>(`ECHO\n`),
12
of<ArrayBuffer | string>(`Method: ${request.method}\n`),
13
of<ArrayBuffer | string>(`Path: ${request.url}\n`),
14
request.raw
15
]).pipe(flatMap(v => v))
16
}))
17
)
18
.listen(81);
19
​
Copied!
And the response will look like:
1
> curl -F 'data=DATA_DATA_DATA_DATA' http://localhost:81
Copied!
1
ECHO
2
Method: POST
3
Path: /
4
--------------------------c631848d1807aa8f
5
Content-Disposition: form-data; name="data"
6
​
7
DATA_DATA_DATA_DATA
8
--------------------------c631848d1807aa8f--
Copied!
To fully read the example see also:
Last modified 2yr ago
Copy link