This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (84 loc) · 2.93 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const express = require('express');
const mongoose = require('mongoose');
// set up express app
const app = express();
const fs = require('fs');
const http = require('http');
const https = require('https');
// Please put MongoDB connection string on ./connectionString.txt
const connectionString = fs.readFileSync('./connectionString.txt', 'utf8')
// get local ip
var os = require('os');
var interfaces = os.networkInterfaces();
var addresses = [];
for (var k in interfaces) {
for (var k2 in interfaces[k]) {
var address = interfaces[k][k2];
if (address.family === 'IPv4' && !address.internal) {
addresses.push(address.address);
}
}
}
// HTTPS?
const isHTTPS = false
// connect to mongodb
mongoose.connect(connectionString, { useNewUrlParser: true, useUnifiedTopology: true});
mongoose.Promise = global.Promise;
//app.use(function(req, res, next) {
// if ((req.get('X-Forwarded-Proto') !== 'https')) {
// res.redirect('https://' + req.get('Host') + req.url);
// } else
// next();
// });
// use body-parser middleware
app.use(express.json({ limit: '100MB'}));
// initialize routes
if (isHTTPS) {
app.use (function (req, res, next) {
if (req.secure) {
// request was via https, so do no special handling
next();
} else {
// request was via http, so redirect to https
res.redirect('https://' + req.headers.host + req.url);
}
});
}
app.use('/api', require('./routes/api'));
// def
app.get('/', (req, res) => {
res.redirect('/api/test');
});
// error handling
app.use(function(err, req, res, next){
console.log(err);
res.status(422).send({error: err.message});
});
// Start server
if (isHTTPS) {
// Certificate for HTTPS
const privateKey = fs.readFileSync('/etc/letsencrypt/live/stnk-api-ta.tech/privkey.pem', 'utf8');
const certificate = fs.readFileSync('/etc/letsencrypt/live/stnk-api-ta.tech/cert.pem', 'utf8');
const ca = fs.readFileSync('/etc/letsencrypt/live/stnk-api-ta.tech/chain.pem', 'utf8');
const credentials = {
key: privateKey,
cert: certificate,
ca: ca
};
const httpServer = http.createServer(app);
const httpsServer = https.createServer(credentials, app);
// listen for request: https and http
httpServer.listen(80, () => {
console.log('HTTP Server running on port 80');
});
httpsServer.listen(443, () => {
console.log('HTTPS Server running on port 443\n');
});
} else {
// listen for requests
app.listen(process.env.port || 80, function(){
console.log('HTTP server running on port 80\n');
});
}
console.log("\nLocal IP:")
console.log(addresses);