Skip to content

Instantly share code, notes, and snippets.

@xiaojay
Created June 1, 2019 07:17
Show Gist options
  • Save xiaojay/d56f4c475f4cfd32bb2e15418f98dc1e to your computer and use it in GitHub Desktop.
Save xiaojay/d56f4c475f4cfd32bb2e15418f98dc1e to your computer and use it in GitHub Desktop.
log out http request
const http = require('http');
http.createServer((request, response) => {
const { headers, method, url } = request;
let body = [];
request.on('error', (err) => {
console.error(err);
}).on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
// BEGINNING OF NEW STUFF
response.on('error', (err) => {
console.error(err);
});
response.statusCode = 200;
response.setHeader('Content-Type', 'application/json');
// Note: the 2 lines above could be replaced with this next one:
// response.writeHead(200, {'Content-Type': 'application/json'})
const responseBody = { headers, method, url, body };
console.log(responseBody)
console.log('\n')
response.write(JSON.stringify(responseBody));
response.end();
// Note: the 2 lines above could be replaced with this next one:
// response.end(JSON.stringify(responseBody))
// END OF NEW STUFF
});
}).listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment