Anarchy_Streaming/server.js

95 lines
2.0 KiB
JavaScript
Raw Normal View History

2023-02-18 18:10:28 +01:00
const http = require("http");
const fs = require("fs");
const qs = require("querystring");
const db = require("./data/data.json");
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 === "/") {
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();
});
}
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) {
res.writeHead(301, { Location: "/home" });
} else {
res.writeHead(301, { Location: "/wrong_info" });
}
});
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}`));