Last active
June 25, 2021 04:54
-
-
Save oieioi/6a19aaa2e00668713042a7bc2ecbf6f5 to your computer and use it in GitHub Desktop.
ローカルでたてて適当なHTTPを受け付けるやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http') | |
const SLEEP_MSEC = 1000; | |
let count = 1; | |
console.log('hello, world') | |
const server = http.createServer((req, res) => { | |
count++ | |
const number = count | |
console.log(`req: ${number} | |
path: ${req.url} | |
method: ${req.method}`) | |
let postData = ''; | |
req.on('data', function(chunk) { | |
postData += chunk; | |
}).on('end', function() { | |
console.log(` body: ${postData}`); | |
try { | |
JSON.parse(postData); | |
console.log(" this body is valid JSON."); | |
} catch(e) { | |
console.log(" this body is invalid JSON."); | |
} | |
}) | |
setTimeout(() => { | |
res.end('ok'); | |
console.log('res:' + number) | |
}, SLEEP_MSEC) | |
}) | |
server.listen(8080) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使用例