Last active
January 1, 2016 16:09
-
-
Save yuta-imai/8168675 to your computer and use it in GitHub Desktop.
8080番ポートでHTTPリクエストを受け付け、受け取ったリクエストのヘッダとボディをJSONにして出力するサンプルコード。
This file contains hidden or 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
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