-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
29 lines (23 loc) · 736 Bytes
/
server.js
File metadata and controls
29 lines (23 loc) · 736 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const http = require("http");
const fs = require("fs");
const path = require("path");
const server = http.createServer((req, res) => {
let filePath = req.url === "/" ? "index.html" : req.url;
const ext = path.extname(filePath);
let contentType = "text/html";
if (ext === ".css") contentType = "text/css";
if (ext === ".js") contentType = "text/javascript";
const fullPath = path.join(__dirname, filePath);
fs.readFile(fullPath, (err, content) => {
if (err) {
res.writeHead(404);
res.end("Not found");
return;
}
res.writeHead(200, { "Content-Type": contentType });
res.end(content);
});
});
server.listen(3000, () => {
console.log("Server running at http://localhost:3000");
});