-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.js
30 lines (24 loc) · 805 Bytes
/
node.js
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
const http = require('http');
const fs = require('fs');
const path = require('path');
const server = http.createServer((req, res) => {
let filePath = path.join(__dirname, 'index.html');
let contentType = 'text/html';
if (req.url === '/script.js') {
filePath = path.join(__dirname, 'script.js');
contentType = 'text/javascript';
}
fs.readFile(filePath, (err, content) => {
if (err) {
res.writeHead(500);
res.end(`Server Error: ${err}`);
} else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
});
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});