| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,4 +4,5 @@ | |||
| 4 | 4 | * [Introduction](ja/introduction/README.md) | |
| 5 | 5 | * [jQuery](ja/jQuery/README.md) | |
| 6 | 6 | * [ESLint](ja/ESLint/README.md) | |
| 7 | + * [connect](ja/connect/README.md) | ||
| 7 | 8 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,4 +9,7 @@ _middleware_という拡張する仕組みを持っていて、connectが持つ | |||
| 9 | 9 | ||
| 10 | 10 | ## どう書ける? | |
| 11 | 11 | ||
| 12 | + Connectを使った簡単なEchoサーバを書いてみましょう。 | ||
| 13 | + Echoサーバとは、送られてきたリクエストの内容をそのままレスポンスとして返すサーバのことです。 | ||
| 14 | + | ||
| 12 | 15 | [import, connect-inline-example.js](../../src/connect/connect-inline-example.js) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,8 +1,13 @@ | |||
| 1 | 1 | "use strict"; | |
| 2 | - import assert from "assert"; | ||
| 3 | 2 | import connect from "connect"; | |
| 4 | 3 | import http from "http"; | |
| 5 | 4 | 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 | + }); | ||
| 6 | 11 | // add "X-Content-Type-Options" to response | |
| 7 | 12 | app.use(function (req, res, next) { | |
| 8 | 13 | res.setHeader("X-Content-Type-Options", "nosniff"); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,6 @@ | |||
| 1 | + "use strict"; | ||
| 2 | + export default function () { | ||
| 3 | + return function (req, res) { | ||
| 4 | + req.pipe(res); | ||
| 5 | + }; | ||
| 6 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,3 @@ | |||
| 1 | - // LICENSE : MIT | ||
| 2 | 1 | "use strict"; | |
| 3 | 2 | export default function (text) { | |
| 4 | 3 | return function (req, res) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,7 @@ import connect from "connect"; | |||
| 5 | 5 | import errorHandler from "../../src/connect/errorHandler"; | |
| 6 | 6 | import nosniff from "../../src/connect/nosniff"; | |
| 7 | 7 | import hello from "../../src/connect/hello"; | |
| 8 | + import echo from "../../src/connect/echo"; | ||
| 8 | 9 | import http from "http"; | |
| 9 | 10 | import fetch from "node-fetch"; | |
| 10 | 11 | describe("connect", function () { | |
@@ -66,4 +67,32 @@ describe("connect", function () { | |||
| 66 | 67 | }); | |
| 67 | 68 | }); | |
| 68 | 69 | }); | |
| 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 | + }); | ||
| 69 | 98 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments