-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
63 lines (51 loc) · 1.72 KB
/
server.js
File metadata and controls
63 lines (51 loc) · 1.72 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const http = require('http');
const fs = require('fs');
const path = require('path');
const url = require('url');
const server = http.createServer((req, res) => {
// Parse the request URL
const parsedUrl = url.parse(req.url, true);
// Set the content type to HTML by default
let contentType = 'text/html';
// Map file extensions to content types
const mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.jpeg': 'image/jpeg',
'.jpg': 'image/jpeg',
'.png': 'image/png',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.json': 'application/json', // Added JSON content type
};
// Get the file extension from the URL
const ext = path.extname(parsedUrl.pathname);
// Check if the extension is in the map, set content type accordingly
if (mimeTypes[ext]) {
contentType = mimeTypes[ext];
}
// Construct the file path
let filePath = path.join(__dirname, parsedUrl.pathname);
// If the filePath is a directory, append 'index.html'
if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) {
filePath = path.join(filePath, 'index.html');
}
// Check if the file exists before creating a read stream
if (fs.existsSync(filePath)) {
// Read the static file
const readStream = fs.createReadStream(filePath);
// Set the content type header
res.setHeader('Content-Type', contentType);
// Pipe the file to the response
readStream.pipe(res);
} else {
// Respond with a 404 Not Found if the file doesn't exist
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 Not Found');
}
});
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});