-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathconnect-echo-example.js
More file actions
33 lines (32 loc) · 886 Bytes
/
connect-echo-example.js
File metadata and controls
33 lines (32 loc) · 886 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import connect from "connect";
import http from "http";
import fetch from "node-fetch";
import assert from "assert";
const app = connect();
// add Error handling
app.use(function (err, req, res, next) {
console.error(err.stack);
res.status(500).send(err.message);
next();
});
// request to response
app.use(function (req, res) {
req.pipe(res);
});
//create node.js http server and listen on port
const server = http.createServer(app).listen(3000, request);
// request => response
function request() {
const closeServer = server.close.bind(server);
const requestBody = {
"key": "value"
};
fetch("http://localhost:3000", {
method: "POST",
body: JSON.stringify(requestBody)
})
.then(res => res.text())
.then(text => {
assert.deepEqual(text, requestBody);
}).then(closeServer, closeServer);
}