forked from demipixel/autotorio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
50 lines (38 loc) · 1.43 KB
/
app.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
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const cookieParser = require('cookie-parser');
const http = require('http');
const https = require('https');
const fs = require('fs');
const PORT = process.env.NODE_ENV != 'production' ? 5000 : 443;
const app = express();
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use('/assets', express.static(path.join(__dirname, 'assets')));
app.use('/favicon.ico', express.static('assets/img/favicon.ico'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(cookieParser());
const certPath = '/etc/letsencrypt/live/autotorio.com/fullchain.pem';
const keyPath = '/etc/letsencrypt/live/autotorio.com/privkey.pem';
const server = (process.env.NODE_ENV == 'production' ? https.createServer({
cert: fs.existsSync(certPath) ? fs.readFileSync(certPath) : undefined,
key: fs.existsSync(keyPath) ? fs.readFileSync(keyPath) : undefined
}, app) : http.createServer(app)).listen(PORT, () => {
console.log('Server live on port ' + PORT);
});
function requireHTTPS(req, res, next) {
if (!req.secure && process.env.NODE_ENV == 'production') {
return res.redirect('https://' + req.get('host') + req.url);
}
next();
}
if (process.env.NODE_ENV == 'production') {
app.listen(80);
app.use(requireHTTPS);
}
require('./router')(app);