Skip to content

Instantly share code, notes, and snippets.

@pjanuario
Created May 23, 2018 09:15
Show Gist options
  • Save pjanuario/beedd1e8bbdaf26f1da4147524aaab09 to your computer and use it in GitHub Desktop.
Save pjanuario/beedd1e8bbdaf26f1da4147524aaab09 to your computer and use it in GitHub Desktop.
Simple HTTP server to output request url and post body
// content of index.js
const http = require('http')
const port = 3000
const requestHandler = (request, response) => {
console.log("URL: %s", request.url)
if (request.method === 'POST') {
let body = '';
request.on('data', chunk => {
body += chunk.toString(); // convert Buffer to string
});
request.on('end', () => {
console.log("body => %s", body);
response.end('ok');
});
}
}
const server = http.createServer(requestHandler)
server.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment