| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,33 @@ | |||
| 1 | + "use strict"; | ||
| 2 | + import connect from "connect"; | ||
| 3 | + import http from "http"; | ||
| 4 | + import assert from "assert"; | ||
| 5 | + var app = 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 | + var server = http.createServer(app).listen(3000, request); | ||
| 18 | + | ||
| 19 | + // request => response | ||
| 20 | + function request() { | ||
| 21 | + var requestBody = { | ||
| 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 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,17 +8,20 @@ import http from "http"; | |||
| 8 | 8 | import fetch from "node-fetch"; | |
| 9 | 9 | const responseText = "response text"; | |
| 10 | 10 | var app = connect(); | |
| 11 | + // add Error handling | ||
| 11 | 12 | app.use(errorHandler()); | |
| 13 | + // add "X-Content-Type-Options" to response | ||
| 12 | 14 | app.use(nosniff()); | |
| 15 | + // respond to all requests | ||
| 13 | 16 | app.use(hello(responseText)); | |
| 17 | + //create node.js http server and listen on port | ||
| 18 | + var server = http.createServer(app).listen(3000, request); | ||
| 14 | 19 | ||
| 15 | - var server = http.createServer(app).listen(3000, () => { | ||
| 20 | + function request() { | ||
| 16 | 21 | fetch("http://localhost:3000") | |
| 17 | 22 | .then(res => res.text()) | |
| 18 | 23 | .then(text => { | |
| 19 | 24 | assert.equal(text, responseText); | |
| 20 | 25 | server.close(); | |
| 21 | 26 | }).catch(console.error.bind(console)); | |
| 22 | - }); | ||
| 23 | - | ||
| 24 | - | ||
| 27 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments