-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
35 lines (32 loc) · 910 Bytes
/
Copy pathserver.js
File metadata and controls
35 lines (32 loc) · 910 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 server = http.createServer((req, res) => {
if (req.url === '/' || req.url === '/index.html') {
fs.readFile('demo-form.html', (err, data) => {
if (err) {
res.writeHead(500);
res.end('Error loading demo form');
return;
}
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
});
} else {
res.writeHead(404);
res.end('Not found');
}
});
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Demo server running at http://localhost:${PORT}`);
console.log('Press Ctrl+C to stop the server');
});
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.log(`Port ${PORT} is already in use. Trying port ${PORT + 1}`);
server.listen(PORT + 1);
} else {
console.error('Server error:', err);
}
});