Chapter 9: Malicious Input samples

This commit is contained in:
Beth Griggs
2020-09-06 00:20:09 +01:00
parent d8520f9bea
commit a77f753e2f
7 changed files with 558 additions and 0 deletions
+34
View File
@@ -0,0 +1,34 @@
const http = require("http");
const { STATUS_CODES } = http;
const server = http.createServer((req, res) => {
if (req.method === "POST" && req.url === "/") {
greeting(req, res);
return;
}
res.statusCode = 404;
res.end(STATUS_CODES[res.statusCode]);
});
greeting = (req, res) => {
let data = "";
req.on("data", (chunk) => (data += chunk));
req.on("end", () => {
try {
data = JSON.parse(data);
} catch (e) {
res.end('{"msg": ""}');
return;
}
if (data.hasOwnProperty("name")) {
res.end(`${data.msg} ${data.name}`);
} else {
res.end(data.msg);
}
});
};
server.listen(3000);