This repository was archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (43 loc) · 1.89 KB
/
server.js
File metadata and controls
55 lines (43 loc) · 1.89 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
const express = require('express');
const Http = require('http');
const Https = require('https');
const fs = require('fs');
const mongoose = require('mongoose');
const subscribeapi = require('./subscribeapi');
const queryapi = require('./queryapi');
const tasks = require('./tasks');
const bodyParser = require('body-parser');
const Config = require('./config.js');
let DB_URL = Config.mongodb_server_url;
console.log("Connecting to " + DB_URL + "...");
mongoose.connect(DB_URL, { useMongoClient: true }).then(
() => {
console.log("Connected to database");
let app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/subscribe', subscribeapi);
app.use('/', queryapi);
let httpServer = Http.createServer(app);
httpServer.listen(process.env.PORT || Config.server_http_port, () => {
let host = httpServer.address().address;
let port = httpServer.address().port;
console.log("Http Server running at http://%s:%s", host, port);
tasks.scheduleQueryTask();
});
if (Config.enable_https) {
console.log("Https Server is enabled. Now loading credentials...");
let privateKey = fs.readFileSync(Config.private_key_path, 'utf8');
let certificate = fs.readFileSync(Config.certificate_path, 'utf8');
let credentials = {key: privateKey, cert: certificate};
let httpsServer = Https.createServer(credentials, app);
httpsServer.listen(Config.server_https_port, () => {
let host = httpsServer.address().address;
let port = httpsServer.address().port;
console.log("Https Server running at https://%s:%s", host, port);
});
}
},
err => { console.log(err.message) }
);
mongoose.connection.on('disconnected', () => { console.log('Disconnected.')});