Skip to content

Instantly share code, notes, and snippets.

@oieioi
Last active June 25, 2021 04:54
Show Gist options
  • Save oieioi/6a19aaa2e00668713042a7bc2ecbf6f5 to your computer and use it in GitHub Desktop.
Save oieioi/6a19aaa2e00668713042a7bc2ecbf6f5 to your computer and use it in GitHub Desktop.
ローカルでたてて適当なHTTPを受け付けるやつ
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)
@oieioi
Copy link
Author

oieioi commented Jun 15, 2021

使用例

node server.js &
curl localhost:8080
curl "localhost:8080/search?hoge=true"
curl -X POST --data '{"hoge": true}' localhost:8080

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment