Skip to content

Commit 95025c6

Browse files
committed
feat(connect): add echo example
1 parent e1e7e44 commit 95025c6

File tree

3 files changed

+40
-27
lines changed

3 files changed

+40
-27
lines changed

src/connect/connect-echo-example.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"use strict";
2+
import connect from "connect";
3+
import http from "http";
4+
import assert from "assert";
5+
var app = connect();
6+
// add Error handling
7+
app.use(function (err, req, res, next) {
8+
console.error(err.stack);
9+
res.status(500).send(err.message);
10+
next();
11+
});
12+
// request to response
13+
app.use(function (req, res) {
14+
req.pipe(res);
15+
});
16+
//create node.js http server and listen on port
17+
var server = http.createServer(app).listen(3000, request);
18+
19+
// request => response
20+
function request() {
21+
var requestBody = {
22+
"key": "value"
23+
};
24+
fetch("http://localhost:3000", {
25+
method: "POST",
26+
body: JSON.stringify(requestBody)
27+
})
28+
.then(res => res.text())
29+
.then(text => {
30+
assert.deepEqual(text, requestBody);
31+
server.close();
32+
}).catch(console.error.bind(console));
33+
}

src/connect/connect-example.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ import http from "http";
88
import fetch from "node-fetch";
99
const responseText = "response text";
1010
var app = connect();
11+
// add Error handling
1112
app.use(errorHandler());
13+
// add "X-Content-Type-Options" to response
1214
app.use(nosniff());
15+
// respond to all requests
1316
app.use(hello(responseText));
17+
//create node.js http server and listen on port
18+
var server = http.createServer(app).listen(3000, request);
1419

15-
var server = http.createServer(app).listen(3000, () => {
20+
function request() {
1621
fetch("http://localhost:3000")
1722
.then(res => res.text())
1823
.then(text => {
1924
assert.equal(text, responseText);
2025
server.close();
2126
}).catch(console.error.bind(console));
22-
});
23-
24-
27+
}

src/connect/connect-inline-example.js

-23
This file was deleted.

0 commit comments

Comments
 (0)