Skip to content

Instantly share code, notes, and snippets.

@yuta-imai
Last active January 1, 2016 16:09
Show Gist options
  • Save yuta-imai/8168675 to your computer and use it in GitHub Desktop.
Save yuta-imai/8168675 to your computer and use it in GitHub Desktop.
8080番ポートでHTTPリクエストを受け付け、受け取ったリクエストのヘッダとボディをJSONにして出力するサンプルコード。
var http = require('http');
http.createServer(function(req,res){
var data = {
RequestHeader: req.headers
};
if(req.method == 'GET'){
response(res,data);
}else if(req.method == 'POST'){
req.on('data',function(body){
data.RequestBody = body.toString();
req.on('end',function(){
response(res,data);
});
});
}
}).listen(8080);
function response(res,data){
var json = JSON.stringify(data);
res.writeHead(200,{'Content-Type':'application/json','Content-Length':json.length});
res.end(json);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment