forked from ProbableTrain/MapGenerator
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhttps_server.js
More file actions
73 lines (62 loc) · 2.07 KB
/
https_server.js
File metadata and controls
73 lines (62 loc) · 2.07 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
64
65
66
67
68
69
70
71
72
73
const express = require('express');
const path = require('path');
const fs = require('fs');
const https = require('https');
const app = express();
const defaultPort = 8000;
let port = process.env.PORT || defaultPort;
// Paths to SSL/TLS key and certificate
const sslOptions = {
key: fs.readFileSync(path.join(__dirname, 'ssl/private-key.pem')), // Path to private key
cert: fs.readFileSync(path.join(__dirname, 'ssl/certificate.pem')), // Path to certificate
};
// Serve static files from the "dist" directory
app.use(express.static(path.join(__dirname, "dist/")));
// Example route
app.get('/', (req, res) => {
res.send('NOOP');
});
function startServer(currentPort) {
const server = https.createServer(sslOptions, app);
// Start listening on the specified port
server.listen(currentPort, () => {
console.log(`HTTPS server running at https://localhost:${currentPort}`);
});
// Handle server errors
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.log(`Port ${currentPort} is in use. Trying another port...`);
server.close(() => {
startServer(currentPort + 1); // Try the next port
});
} else {
console.error('Server error:', err);
process.exit(1);
}
});
// Handle graceful shutdown on SIGINT (Ctrl+C)
process.on('SIGINT', () => {
console.log('Stopping server...');
server.close(() => {
console.log('Server stopped.');
process.exit();
});
});
// Handle uncaught exceptions globally
process.on('uncaughtException', (err) => {
console.error('Uncaught Exception:', err);
process.exit(1);
});
// Handle unhandled promise rejections globally
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
process.exit(1);
});
}
// Start the server
try {
startServer(port);
} catch (err) {
console.error('Error starting server:', err);
process.exit(1);
}