-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
35 lines (29 loc) · 812 Bytes
/
server.js
File metadata and controls
35 lines (29 loc) · 812 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
30
31
32
33
34
35
const http = require('http');
const fs = require('fs');
const path = require('path');
const port = 3333;
const server = http.createServer((req, res) => {
const filePath = req.url === '/' ? './index.html' : `.${req.url}`;
const extname = path.extname(filePath);
let contentType = 'text/html';
// Set the content type based on the file extension
switch (extname) {
case '.css':
contentType = 'text/css';
break;
case '.js':
contentType = 'text/javascript';
break;
}
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(500);
return res.end('Error loading file');
}
res.writeHead(200, { 'Content-Type': contentType });
res.end(data);
});
});
server.listen(port, () => {
console.log(`Server running on port ${port}`);
});