basic node server

+ very interesting back and forth in TS/JS configs 🤡
This commit is contained in:
LuckyLaszlo
2022-10-26 19:27:06 +02:00
parent e56c8e0b67
commit a831b7954c
13 changed files with 629 additions and 554 deletions

40
src/server/server.ts Normal file
View File

@@ -0,0 +1,40 @@
// @ts-check
// var http = require("http");
// var url = require("url");
// var fs = require("fs");
// var path = require("path");
import http from "http";
import url from "url";
import fs from "fs";
import path from "path";
const hostname = "localhost";
const port = 8080;
const root = "/mnt/c/Users/Lucky/Desktop/code/ft_transcendence/www/";
const server = http.createServer((req, res) => {
// var q = new URL(req.url, `http://${req.getHeaders().host}`)
// @ts-ignore
var q = url.parse(req.url, true);
var filename = root + q.pathname;
fs.readFile(filename, (err, data) => {
if (err) {
res.writeHead(404, {"Content-Type": "text/html"});
return res.end("404 Not Found");
}
if (path.extname(filename) === ".html") {
res.writeHead(200, {"Content-Type": "text/html"});
}
if (path.extname(filename) === ".js") {
res.writeHead(200, {"Content-Type": "application/javascript"});
}
res.write(data);
return res.end();
});
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});