Accept data only at /post

This commit is contained in:
powermaker450 2024-08-18 19:22:50 -04:00
parent d865b56c63
commit 14791149c6

View file

@ -15,9 +15,11 @@ const contentType = { "Content-Type": "application/json" };
http http
.createServer((req, res) => { .createServer((req, res) => {
const isPost = req.method === "POST"; const isPost = req.method === "POST";
const sender = emp(req.headers["user-agent"]);
req.on("data", async (chunk) => { req.on("data", async (chunk) => {
const sender = emp(req.headers["user-agent"]);
if (req.url === "/post") {
let data: Review[] = checkFile("data.json", "utf8"); let data: Review[] = checkFile("data.json", "utf8");
let temp: any; let temp: any;
@ -48,19 +50,23 @@ http
.catch((err) => { .catch((err) => {
logger.error(err); logger.error(err);
res.writeHead(415, contentType); res.writeHead(400, contentType);
res.write(JSON.stringify({ response: "error", message: "Invalid content-type." })); res.write(JSON.stringify({ response: "error", message: "Invalid content-type." }));
res.end(); res.end();
}); });
} else {
logger.log(`${sender} cannot ${req.method} to ${req.url}`);
res.writeHead(400, contentType);
res.write(response.error(`Cannot ${req.method} to '${req.url}'`));
res.end();
}
}); });
req.on("end", () => { req.on("end", () => {
const sender = emp(req.headers["user-agent"]);
if (!isPost) { if (!isPost) {
logger.log(`${sender} sent no data.`) res.writeHead(404, {"Content-Type": "text/plain"});
res.write(`Cannot ${req.method} ${req.url}`);
res.writeHead(400, contentType);
res.write(response.error("Invalid content-type"));
res.end(); res.end();
} }
}); });