-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
47 lines (40 loc) · 1.44 KB
/
server.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
const express = require("express");
const https = require('https');
const path = require('path');
const fs = require('fs');
const bodyParser = require("body-parser");
if (process.env.NODE_ENV !== 'production') {
require("dotenv").config();
}
const odlAPI = require("./routers/odl");
const conductorAPI = require("./routers/conductor");
const app = express();
app.use(express.static(path.join(__dirname, 'client/build')));
app.use(bodyParser.urlencoded({extended: false}));
app.use("/api/conductor", bodyParser.json(), conductorAPI);
app.use("/api/odl", bodyParser.json(), odlAPI);
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
const port = process.env.NODE_PORT || 3000;
const host = process.env.NODE_HOST || "0.0.0.0";
// if certificates folder is empty use HTTP
// else HTTPS is used (key.pem and cert.pem must be present in folder)
fs.readdir('./certificates', function (err, files) {
if (files.length <= 1) {
app.listen(port, host, function () {
console.log("Server is listening at http://%s:%s", host, port);
if (process.send) {
process.send("online");
}
});
} else {
https.createServer({
key: fs.readFileSync('./certificates/key.pem'),
cert: fs.readFileSync('./certificates/cert.pem'),
passphrase: process.env.PASSPHRASE
}, app).listen(port, function () {
console.log('Server is listening on port %s', port)
});
}
});