Skip to content

Instantly share code, notes, and snippets.

@barcellos-pedro
Last active January 21, 2023 21:56
Show Gist options
  • Save barcellos-pedro/2275207c9ebc31bfe9c0484d5b3ade57 to your computer and use it in GitHub Desktop.
Save barcellos-pedro/2275207c9ebc31bfe9c0484d5b3ade57 to your computer and use it in GitHub Desktop.
Nodejs basic server
import { createServer } from "http";
const PORT = 3000;
createServer((request, response) => {
if (request.method !== "POST" || request.url !== "/stream") {
response.status = 404;
response.end("method or url not found");
return;
}
// Pipe request body direct to reponse object
// request.pipe(response);
// or use request events to grab body data
let body = [];
request
.on("error", (err) => console.error(err))
// body data come as buffer <Buffer 7b 0a 20 20 22... >
.on("data", (data) => body.push(data));
.on("end", () => {
body = Buffer.concat(body).toString();
response.end(body);
});
}).listen(PORT, () => console.log(listening on http://localhost:${PORT}`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment