2023-02-18 18:10:28 +01:00
|
|
|
const http = require("http");
|
|
|
|
const fs = require("fs");
|
|
|
|
const qs = require("querystring");
|
2023-02-19 07:14:38 +01:00
|
|
|
const db = require("./data/data.json");
|
2023-02-18 18:10:28 +01:00
|
|
|
|
2023-02-19 07:29:52 +01:00
|
|
|
var logged = "false";
|
|
|
|
|
2023-02-18 18:10:28 +01:00
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
if (req.url === "/styles/styles.css") {
|
|
|
|
res.statusCode = 200;
|
|
|
|
res.setHeader("Content-Type", "text/css");
|
|
|
|
fs.readFile("styles/styles.css", function(error, data) {
|
|
|
|
if (error) {
|
|
|
|
res.writeHead(404);
|
|
|
|
res.write("Error: File not found.");
|
|
|
|
} else {
|
|
|
|
res.write(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.url === "/") {
|
2023-02-19 07:29:52 +01:00
|
|
|
logged = "false";
|
2023-02-18 18:10:28 +01:00
|
|
|
res.statusCode = 200;
|
|
|
|
res.setHeader("Content-Type", "text/html");
|
|
|
|
fs.readFile("pages/landing.html", function(error, data) {
|
|
|
|
if (error) {
|
|
|
|
res.writeHead(404);
|
|
|
|
res.write("Error: File not found.");
|
|
|
|
} else {
|
|
|
|
res.write(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-19 07:14:38 +01:00
|
|
|
if (req.url === "/home") {
|
|
|
|
res.statusCode = 200;
|
|
|
|
res.setHeader("Content-Type", "text/html");
|
|
|
|
fs.readFile("./pages/home.html", (error, data) => {
|
|
|
|
if (error) {
|
|
|
|
res.writeHead(404);
|
|
|
|
res.write("Error: File not found.");
|
|
|
|
} else {
|
|
|
|
res.write(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.url === "/wrong_info") {
|
|
|
|
res.statusCode = 200;
|
|
|
|
res.setHeader("Content-Type", "text/html");
|
|
|
|
fs.readFile("./pages/wronginfo.html", (error, data) => {
|
|
|
|
if (error) {
|
|
|
|
res.writeHead(404);
|
|
|
|
res.write("Error: File not found.");
|
|
|
|
} else {
|
|
|
|
res.write(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.url === "/members" && req.method === "POST") {
|
|
|
|
res.statusCode = 200;
|
|
|
|
//res.setHeader("Content-Type", "text/json");
|
|
|
|
var body = "";
|
|
|
|
|
|
|
|
req.on("data", (data) => {
|
|
|
|
body = body + data;
|
|
|
|
});
|
|
|
|
|
|
|
|
req.on("end", () => {
|
|
|
|
var post = qs.parse(body);
|
|
|
|
db.forEach(i => {
|
|
|
|
if (post.username === i.username && post.password === i.password) {
|
2023-02-19 07:29:52 +01:00
|
|
|
logged = "true";
|
2023-02-19 07:14:38 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-19 07:29:52 +01:00
|
|
|
if (logged === "true") {
|
|
|
|
res.writeHead(301, { Location: "/home" });
|
|
|
|
} else {
|
|
|
|
res.writeHead(301, { Location: "/wrong_info" });
|
|
|
|
}
|
|
|
|
|
2023-02-19 07:14:38 +01:00
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-18 18:10:28 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const port = process.env.port || 9000
|
|
|
|
|
|
|
|
server.listen(port, () => console.log(`Server listening on port ${port}`));
|