Created
May 23, 2018 09:15
-
-
Save pjanuario/beedd1e8bbdaf26f1da4147524aaab09 to your computer and use it in GitHub Desktop.
Simple HTTP server to output request url and post body
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
// 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