Last active
January 21, 2023 21:56
-
-
Save barcellos-pedro/2275207c9ebc31bfe9c0484d5b3ade57 to your computer and use it in GitHub Desktop.
Nodejs basic 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
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