Skip to content

Instantly share code, notes, and snippets.

@heygema
Created September 2, 2020 17:04
Show Gist options
  • Save heygema/a0f6f7e8d2ca6f9e42e7174415e1a238 to your computer and use it in GitHub Desktop.
Save heygema/a0f6f7e8d2ca6f9e42e7174415e1a238 to your computer and use it in GitHub Desktop.
logServer (next milestone is auto-generate types for yup)
const fs = require("fs");
const http = require("http");
http
.createServer((req, res) => {
res.setHeader("Content-Type", "application/json");
let endMsg = JSON.stringify({
message: "Request Finished"
});
switch (req.url) {
case "/log":
if (req.method !== "POST") {
res.statusCode = 404;
res.end(endMsg);
// res.end(JSON.stringify({ message: "Invalid Method" }));
}
let body = "";
req.on("data", chunk => {
body += chunk.toString();
});
req.on("end", () => {
fs.writeFile(
"./logs.json",
JSON.stringify(JSON.parse(body), null, 2),
(err, _) => {
if (err) {
res.end(
JSON.stringify({
message: "Writing file error"
})
);
}
res.end(JSON.stringify({ message: "finished" }));
}
);
res.statusCode = 200;
res.end(endMsg);
});
default:
res.statusCode = 404;
res.end(endMsg);
}
})
.listen(8091, () => {
console.log("running on 8091");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment