-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
73 lines (60 loc) · 2.14 KB
/
Copy pathserver.js
File metadata and controls
73 lines (60 loc) · 2.14 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const express = require("express");
//const https = require("https");
//const crypto = require("crypto");
//const fs = require('fs');
const cors = require('cors');
const path = require('path')
const app = express();
//some lines are commented out
//login/register, API and secure connection are only partly prepared
/*var config;
fs.readFile('./config/config.json', 'utf8', (err, data) => {
if (err) {
console.log(`Error reading config: ${err}`);
}else{
config = JSON.parse(data);
if (config.jwt.secret_key === 'none') {
config.jwt.secret_key = crypto.randomBytes(64).toString('hey');
fs.writeFile('./config/config.json', JSON.stringify(config, null, 4), (err) => {
if (err) {
console.log(`Error writing config: ${err}`);
}
});
}
}
});*/
//set up Port
//const port_https = process.env.PORT || 443;
const port_http = process.env.PORT || 80;
//set up certs for https
/*var key = fs.readFileSync(__dirname + '/certs/server.key');
var cert = fs.readFileSync(__dirname + '/certs/server.crt');
var options = {
key: key,
cert: cert
};*/
//TODO at install generate valid certs
//setting view engine to ejs
app.set("view engine", "ejs");
app.set("case sensitive routing", false);
//add required modules
app.use(express.json());
app.use(cors());
/*app.use((req, res, next) => {
if (req.headers['x-forwarded-proto'] !== 'https') {
return res.redirect('https://' + req.headers.host + req.url);
}else{
return next();
}
});*/
//static route for resources like pictures and css
app.use('/resource', express.static(path.join(__dirname, 'public')));
//Router
const api_router = require('./router/api_router');
app.use('/api', api_router);
const site_router = require('./router/site_router');
app.get('*', site_router);
app.set('views', path.join(__dirname, 'views/'));
//var server_https = https.createServer (options, app);
//server_https.listen(port_https, 'localhost', () => console.log(`Server running on port ${port_https}`));
app.listen(port_http, 'localhost', () => console.log(`Server running on port ${port_http}`));