Accept data only at /post
This commit is contained in:
parent
d865b56c63
commit
14791149c6
72
src/main.ts
72
src/main.ts
|
@ -15,52 +15,58 @@ 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"]);
|
|
||||||
let data: Review[] = checkFile("data.json", "utf8");
|
|
||||||
let temp: any;
|
|
||||||
|
|
||||||
try {
|
if (req.url === "/post") {
|
||||||
temp = JSON.parse(chunk);
|
let data: Review[] = checkFile("data.json", "utf8");
|
||||||
} catch (err) {
|
let temp: any;
|
||||||
console.error(origin + "Chunk is not valid JSON");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
await reviewSchema
|
try {
|
||||||
.validate(temp)
|
temp = JSON.parse(chunk);
|
||||||
.then((valid) => {
|
} catch (err) {
|
||||||
req.on("end", () => {
|
console.error(origin + "Chunk is not valid JSON");
|
||||||
logger.log(`${sender} sent:`, valid);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
data.push(valid);
|
await reviewSchema
|
||||||
fs.writeFileSync(
|
.validate(temp)
|
||||||
"./persist/data.json",
|
.then((valid) => {
|
||||||
JSON.stringify(data, null, 2),
|
req.on("end", () => {
|
||||||
);
|
logger.log(`${sender} sent:`, valid);
|
||||||
|
|
||||||
res.writeHead(201, contentType);
|
data.push(valid);
|
||||||
res.write(response.success("Review was sent"));
|
fs.writeFileSync(
|
||||||
|
"./persist/data.json",
|
||||||
|
JSON.stringify(data, null, 2),
|
||||||
|
);
|
||||||
|
|
||||||
|
res.writeHead(201, contentType);
|
||||||
|
res.write(response.success("Review was sent"));
|
||||||
|
res.end();
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
logger.error(err);
|
||||||
|
|
||||||
|
res.writeHead(400, contentType);
|
||||||
|
res.write(JSON.stringify({ response: "error", message: "Invalid content-type." }));
|
||||||
res.end();
|
res.end();
|
||||||
});
|
});
|
||||||
})
|
} else {
|
||||||
.catch((err) => {
|
logger.log(`${sender} cannot ${req.method} to ${req.url}`);
|
||||||
logger.error(err);
|
|
||||||
|
|
||||||
res.writeHead(415, contentType);
|
res.writeHead(400, contentType);
|
||||||
res.write(JSON.stringify({ response: "error", message: "Invalid content-type." }));
|
res.write(response.error(`Cannot ${req.method} to '${req.url}'`));
|
||||||
res.end();
|
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();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue