Last active
November 2, 2022 05:39
-
-
Save codegino/947e603ead3c7752d45b9e9b697e6281 to your computer and use it in GitHub Desktop.
Vanilla Node.js Server
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 PORT = 8088; | |
const HOST = "localhost"; | |
const server = http.createServer(function handleRequest(req, res) { | |
res.writeHead(200, { "Content-Type": "application/json" }); | |
res.end(JSON.stringify({ message: "hello world" })); | |
}); | |
server.listen(PORT, HOST, () => { | |
console.log(`Server on ${HOST}:${PORT}`); | |
}); |
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 HOST = "localhost"; | |
const PORT = 8088; | |
const server = http.createServer((req, res) => { | |
if (req.method === "POST") { | |
let body = ""; | |
req.on("data", (chunk) => { | |
body += chunk; | |
}); | |
req.on("close", () => { | |
console.log(body); | |
}); | |
res.writeHead(201); | |
res.end("ok"); | |
} else { | |
res.writeHead(200); | |
res.end("Not a POST request"); | |
} | |
}); | |
server.listen(PORT, HOST, () => { | |
console.log(`Server on ${HOST}:${PORT}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment