basic node server
+ very interesting back and forth in TS/JS configs 🤡
This commit is contained in:
40
src/server/server.ts
Normal file
40
src/server/server.ts
Normal 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}/`);
|
||||
});
|
||||
Reference in New Issue
Block a user