Add HTTPS server (self-signed fallback), update frontend messages, and README
This commit is contained in:
34
server.js
34
server.js
@@ -57,6 +57,30 @@ const m2m100LangMap = {
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3000;
|
||||
|
||||
// HTTPS setup (optional). Serve HTTPS on 3443 using provided certs or generate a self-signed cert.
|
||||
const https = require('https');
|
||||
const fs = require('fs');
|
||||
const selfsigned = require('selfsigned');
|
||||
const SSL_PORT = process.env.SSL_PORT || 3443;
|
||||
|
||||
function loadSslOptions() {
|
||||
const keyPath = process.env.SSL_KEY_PATH;
|
||||
const certPath = process.env.SSL_CERT_PATH;
|
||||
|
||||
if (keyPath && certPath && fs.existsSync(keyPath) && fs.existsSync(certPath)) {
|
||||
return {
|
||||
key: fs.readFileSync(keyPath),
|
||||
cert: fs.readFileSync(certPath)
|
||||
};
|
||||
}
|
||||
|
||||
// Generate a temporary self-signed certificate for localhost
|
||||
console.warn('No SSL cert/key provided. Generating a temporary self-signed certificate for https://localhost:' + SSL_PORT);
|
||||
const attrs = [{ name: 'commonName', value: 'localhost' }];
|
||||
const pems = selfsigned.generate(attrs, { days: 365 });
|
||||
return { key: pems.private, cert: pems.cert };
|
||||
}
|
||||
|
||||
// Middleware
|
||||
app.use(cors());
|
||||
app.use(bodyParser.json({ limit: '10mb' }));
|
||||
@@ -478,3 +502,13 @@ app.listen(PORT, () => {
|
||||
========================================
|
||||
`);
|
||||
});
|
||||
|
||||
// Start HTTPS server on SSL_PORT
|
||||
try {
|
||||
const sslOptions = loadSslOptions();
|
||||
https.createServer(sslOptions, app).listen(SSL_PORT, () => {
|
||||
console.log(`HTTPS server running on: https://localhost:${SSL_PORT}`);
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Failed to start HTTPS server:', err);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user