@devchristian1337/dev-icons
Version:
A library to serve development-related icons
43 lines (38 loc) • 1.1 kB
JavaScript
const http = require("http");
const fs = require("fs");
const path = require("path");
const server = http.createServer((req, res) => {
let filePath = "." + req.url;
if (filePath === "./") {
filePath = "./test/index.html";
} else if (filePath.startsWith("./src")) {
filePath = "." + req.url;
}
const extname = path.extname(filePath);
const contentType =
{
".html": "text/html",
".js": "text/javascript",
".css": "text/css",
".json": "application/json",
".svg": "image/svg+xml",
}[extname] || "text/plain";
fs.readFile(filePath, (error, content) => {
if (error) {
if (error.code === "ENOENT") {
res.writeHead(404);
res.end("File not found");
} else {
res.writeHead(500);
res.end("Server error: " + error.code);
}
} else {
res.writeHead(200, { "Content-Type": contentType });
res.end(content, "utf-8");
}
});
});
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}/`);
});