added support for http and https
This commit is contained in:
parent
476e69fe1b
commit
3097255bc7
1 changed files with 29 additions and 8 deletions
37
api.js
37
api.js
|
@ -11,11 +11,7 @@ const gameMap = {};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const sslOptions = {
|
|
||||||
key: fs.readFileSync(path.join(os.homedir(), 'certs', 'privkey.pem')),
|
|
||||||
cert: fs.readFileSync(path.join(os.homedir(), 'certs', 'cert.pem')),
|
|
||||||
ca: fs.readFileSync(path.join(os.homedir(), 'certs', 'fullchain.pem'))
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
data.split('\n').forEach(line => {
|
data.split('\n').forEach(line => {
|
||||||
|
@ -55,6 +51,31 @@ app.get('/contact', (req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
https.createServer(sslOptions, app).listen(PORT, () => {
|
const certDir = path.join(os.homedir(), 'certs');
|
||||||
console.log(`Server running at https://localhost:${PORT}`);
|
const keyPath = path.join(certDir, 'privkey.pem');
|
||||||
});
|
const certPath = path.join(certDir, 'cert.pem');
|
||||||
|
const caPath = path.join(certDir, 'fullchain.pem');
|
||||||
|
|
||||||
|
const areCertsPresent = () => {
|
||||||
|
return fs.existsSync(keyPath) && fs.existsSync(certPath) && fs.existsSync(caPath);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (areCertsPresent()) {
|
||||||
|
console.log('SSL certificates found. Starting HTTPS server...');
|
||||||
|
|
||||||
|
const sslOptions = {
|
||||||
|
key: fs.readFileSync(keyPath),
|
||||||
|
cert: fs.readFileSync(certPath),
|
||||||
|
ca: fs.readFileSync(caPath)
|
||||||
|
};
|
||||||
|
|
||||||
|
https.createServer(sslOptions, app).listen(PORT, () => {
|
||||||
|
console.log(`HTTPS Server running at https://localhost:${PORT}`);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.log('SSL certificates not found. Starting HTTP server...');
|
||||||
|
|
||||||
|
app.listen(PORT, () => {
|
||||||
|
console.log(`HTTP Server running at http://localhost:${PORT}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue