Skip to content

Instantly share code, notes, and snippets.

@codegino
Last active November 2, 2022 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codegino/947e603ead3c7752d45b9e9b697e6281 to your computer and use it in GitHub Desktop.
Save codegino/947e603ead3c7752d45b9e9b697e6281 to your computer and use it in GitHub Desktop.
Vanilla Node.js Server
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}`);
});
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