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

Commit95025c6

Browse files
committed
feat(connect): add echo example
1 parente1e7e44 commit95025c6

File tree

3 files changed

+40
-27
lines changed

3 files changed

+40
-27
lines changed

‎src/connect/connect-echo-example.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"use strict";
2+
importconnectfrom"connect";
3+
importhttpfrom"http";
4+
importassertfrom"assert";
5+
varapp=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+
varserver=http.createServer(app).listen(3000,request);
18+
19+
// request => response
20+
functionrequest(){
21+
varrequestBody={
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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ import http from "http";
88
importfetchfrom"node-fetch";
99
constresponseText="response text";
1010
varapp=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+
varserver=http.createServer(app).listen(3000,request);
1419

15-
varserver=http.createServer(app).listen(3000,()=>{
20+
functionrequest(){
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

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp