Created
September 29, 2021 17:54
-
-
Save mikamboo/90e5a330498a491caaa4e38f47af2b29 to your computer and use it in GitHub Desktop.
NodeJS : Handler POST body using native http createServer
This file contains 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
const http = require("http"); | |
const { parse } = require("querystring"); | |
const hostname = "0.0.0.0"; | |
const port = 3000; | |
const server = http.createServer((req, res) => { | |
if (req.method === "POST") { | |
let body = ""; | |
req.on("data", (chunk) => { | |
// convert Buffer to string | |
body += chunk.toString(); | |
}); | |
req.on("end", () => { | |
console.log(parse(body)); | |
res.setHeader("Content-Type", "application/json"); | |
res.end(JSON.stringify({ data: parse(body) })); | |
}); | |
} else { | |
res.end("Require POST request"); | |
} | |
}); | |
server.listen(port, hostname, () => { | |
console.log(`Server running at http://${hostname}:${port}/`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.digitalocean.com/community/tutorials/how-to-improve-website-performance-using-gzip-and-nginx-on-ubuntu-20-04