Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite1e7e44

Browse files
committed
feat(connect): add "echo" middleware
1 parentff07629 commite1e7e44

File tree

6 files changed

+45
-2
lines changed

6 files changed

+45
-2
lines changed

‎SUMMARY.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
"use strict";
2-
importassertfrom"assert";
32
importconnectfrom"connect";
43
importhttpfrom"http";
54
varapp=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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
exportdefaultfunction(){
3+
returnfunction(req,res){
4+
req.pipe(res);
5+
};
6+
}

‎src/connect/hello.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// LICENSE : MIT
21
"use strict";
32
exportdefaultfunction(text){
43
returnfunction(req,res){

‎test/connect/hello-test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import connect from "connect";
55
importerrorHandlerfrom"../../src/connect/errorHandler";
66
importnosnifffrom"../../src/connect/nosniff";
77
importhellofrom"../../src/connect/hello";
8+
importechofrom"../../src/connect/echo";
89
importhttpfrom"http";
910
importfetchfrom"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+
varapp=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+
varrequestBody={
81+
name:"Hubot",
82+
login:"hubot"
83+
};
84+
returnfetch("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+
returnres.json();
93+
}).then(json=>{
94+
assert.deepEqual(json,requestBody);
95+
});
96+
});
97+
});
6998
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp