Skip to content

Commit

Permalink
feat(connect): middlewareをtraceするサンプルを追加
Browse files Browse the repository at this point in the history
connect-trace-example.js
  • Loading branch information
azu committed Sep 15, 2015
1 parent 089c2af commit 8cb4914
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 20 deletions.
5 changes: 3 additions & 2 deletions ja/connect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Echoサーバとは、送られてきたリクエストの内容をそのまま
```

`app.use(middleware)` という形で、_middleware_と呼ばれる関数には`request``response`といったオブジェクトが渡されます。
そのため、リクエストみてフィルタリングしたり、任意のレスポンスを返したり出来るようになっています。
そのため、リクエストをフィルタリングしたり、ログを取ったり、任意のレスポンスを返したり出来るようになっています。

Echoサーバでは `req.pipe(res);` という形でリクエストをそのままレスポンスとして流す事で実現されています。

Expand All @@ -47,4 +47,5 @@ Echoサーバでは `req.pipe(res);` という形でリクエストをそのま
基本的にどの_middleware_も`app.use(middleware)`という形で拡張でき、
モジュールとして実装すれば再利用もしやすい形となっています。

> **Note** _middleware_となる関数の引数が4つであると、それはエラーハンドリングの_middleware_とするという、Connectの独自のルールがあります。
> **Note** _middleware_となる関数の引数が4つであると、それはエラーハンドリングの_middleware_とするという、Connect独自のルールがあります。
2 changes: 2 additions & 0 deletions src/connect/connect-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import assert from "assert";
import connect from "connect";
import http from "http";
import fetch from "node-fetch";

const responseText = "response text";
let app = connect();
// add Error handling
Expand All @@ -14,6 +15,7 @@ app.use(errorHandler());
app.use(nosniff());
// respond to all requests
app.use(hello(responseText));

//create node.js http server and listen on port
let server = http.createServer(app).listen(3000, request);

Expand Down
21 changes: 7 additions & 14 deletions src/connect/connect-trace-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,10 @@ app.use(nosniff());
// respond to all requests
app.use(hello(responseText));

//create node.js http server and listen on port
let server = http.createServer(app).listen(3000, request);

function request() {
let closeServer = server.close.bind(server);
fetch("http://localhost:3000")
.then(res => res.text())
.then(text => {
assert.equal(text, responseText);
server.close();
})
.catch(console.error.bind(console))
.then(closeServer, closeServer);
}
// print middleware list
app.stack.map(({handle}) => console.log(handle));
/* =>
[Function: errorHandling]
[Function: nosniff]
[Function: hello]
*/
4 changes: 2 additions & 2 deletions src/connect/errorHandler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
export default function errorHandler() {
return function (err, req, res, next) {
export default function () {
return function errorHandling(err, req, res, next) {
console.error(err.stack);
res.status(500).send(err.message);
next();
Expand Down
2 changes: 1 addition & 1 deletion src/connect/hello.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
export default function (text) {
return function (req, res) {
return function hello(req, res) {
res.end(text);
};
}
2 changes: 1 addition & 1 deletion src/connect/nosniff.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function setHeaders(res, headers) {
});
}
export default function () {
return function (req, res, next) {
return function nosniff(req, res, next) {
setHeaders(res, {
"X-Content-Type-Options": "nosniff"
});
Expand Down

0 comments on commit 8cb4914

Please sign in to comment.