Skip to content

Commit e1e7e44

Browse files
committed
feat(connect): add "echo" middleware
1 parent ff07629 commit e1e7e44

File tree

6 files changed

+45
-2
lines changed

6 files changed

+45
-2
lines changed

SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
* [Introduction](ja/introduction/README.md)
55
* [jQuery](ja/jQuery/README.md)
66
* [ESLint](ja/ESLint/README.md)
7+
* [connect](ja/connect/README.md)
78

ja/connect/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ _middleware_という拡張する仕組みを持っていて、connectが持つ
99

1010
## どう書ける?
1111

12+
Connectを使った簡単なEchoサーバを書いてみましょう。
13+
Echoサーバとは、送られてきたリクエストの内容をそのままレスポンスとして返すサーバのことです。
14+
1215
[import, connect-inline-example.js](../../src/connect/connect-inline-example.js)

src/connect/connect-inline-example.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
"use strict";
2-
import assert from "assert";
32
import connect from "connect";
43
import http from "http";
54
var app = connect();
5+
// add Error handling
6+
app.use(function (err, req, res, next) {
7+
console.error(err.stack);
8+
res.status(500).send(err.message);
9+
next();
10+
});
611
// add "X-Content-Type-Options" to response
712
app.use(function (req, res, next) {
813
res.setHeader("X-Content-Type-Options", "nosniff");

src/connect/echo.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
export default function () {
3+
return function (req, res) {
4+
req.pipe(res);
5+
};
6+
}

src/connect/hello.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// LICENSE : MIT
21
"use strict";
32
export default function (text) {
43
return function (req, res) {

test/connect/hello-test.js

+29
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import connect from "connect";
55
import errorHandler from "../../src/connect/errorHandler";
66
import nosniff from "../../src/connect/nosniff";
77
import hello from "../../src/connect/hello";
8+
import echo from "../../src/connect/echo";
89
import http from "http";
910
import fetch from "node-fetch";
1011
describe("connect", function () {
@@ -66,4 +67,32 @@ describe("connect", function () {
6667
});
6768
});
6869
});
70+
describe("echo", function () {
71+
beforeEach(function (done) {
72+
var app = connect();
73+
app.use(echo());
74+
server = http.createServer(app).listen(3000, done);
75+
});
76+
afterEach(function () {
77+
server && server.close();
78+
});
79+
it("should return request as response", function () {
80+
var requestBody = {
81+
name: "Hubot",
82+
login: "hubot"
83+
};
84+
return fetch("http://localhost:3000", {
85+
method: "POST",
86+
headers: {
87+
"Accept": "application/json",
88+
"Content-Type": "application/json"
89+
},
90+
body: JSON.stringify(requestBody)
91+
}).then(res => {
92+
return res.json();
93+
}).then(json => {
94+
assert.deepEqual(json, requestBody);
95+
});
96+
});
97+
});
6998
});

0 commit comments

Comments
 (0)