-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
67 lines (57 loc) · 1.88 KB
/
index.js
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
// Load environment variables from .env file
require('dotenv').config();
/** An optional prefix for the service router: useful if plugging in to an existing Express App */
const ROOT_PREFIX = process.env.ORACLE_ROOT_PREFIX || '';
// Native Node modules
const path = require('path');
const http = require('http');
const https = require('https');
const fs = require('fs');
// NPM modules
const express = require('express');
const { redirectToHTTPS } = require('express-http-to-https');
const cors = require('cors');
// Setup Express
const app = express();
app.use(cors());
const port = process.env.PORT || 3000;
const sslCertPath = '/etc/letsencrypt/live/' + process.env.DOMAIN + '/';
// Plug in our Router
const router = require('./src/routes');
app.use(router);
// Serve static files from the 'public' directory, along with the root prefix if specified
if (ROOT_PREFIX) {
// Prefix specified, serve from it
app.use(ROOT_PREFIX, express.static(path.join(__dirname, 'public')));
} else {
// No prefix, serve from root
app.use(express.static(path.join(__dirname, 'public')));
}
// Startup
let server;
if (process.env.DOMAIN) {
// SSL options
const sslOptions = {
cert: fs.readFileSync(sslCertPath + 'fullchain.pem'),
key: fs.readFileSync(sslCertPath + 'privkey.pem')
};
// Start the HTTPS server
server = https.createServer(sslOptions, app);
server.listen(443, () => {
console.log(`HTTPS server running on port 443 --> https://${process.env.DOMAIN}`);
});
// Listen for HTTP requests for redirect purposes
const httpApp = express();
httpApp.use(redirectToHTTPS());
httpApp.listen(80);
} else {
// Start the HTTP server
server = http.createServer(app);
server.listen(port, () => {
console.log(`HTTP server running on port ${port} --> http://localhost:${port}`);
});
}
// Handle server errors
server.on('error', (error) => {
console.error('Server error:', error);
});