-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
30 lines (26 loc) · 847 Bytes
/
server.js
File metadata and controls
30 lines (26 loc) · 847 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
const http = require('http');
const server = http.createServer((req, res) => {
console.log(`${new Date().toISOString()} - ${req.method} ${req.url}`);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(`
<html>
<body>
<h1>Hello from Node.js!</h1>
<p>Server is running on port ${PORT}</p>
<p>Current time: ${new Date().toISOString()}</p>
</body>
</html>
`);
});
const PORT = process.env.PORT || 3000;
server.on('error', (err) => {
console.error('Server error:', err);
if (err.code === 'EACCES') {
console.error('Permission denied. Try running as root or use a different port.');
} else if (err.code === 'EADDRINUSE') {
console.error(`Port ${PORT} is already in use.`);
}
});
server.listen(PORT, "::", () => {
console.log(`Server running on http://:::${PORT}`);
});