/
index.js
50 lines (39 loc) · 1.16 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
const express = require('express');
const hbs = require('express-handlebars');
const http = require('http');
const https = require('https');
const fs = require('fs');
const routes = require('./routes/routes');
// Certificate
const privateKey = fs.readFileSync('encryption/key.pem');
const certificate = fs.readFileSync('encryption/cert.pem');
const credentials = {
key: privateKey,
cert: certificate,
passphrase: 'preet1234'
};
const app = express();
app.engine('hbs', hbs({
extname: 'hbs',
defaultLayout: 'layout',
layoutsDir: __dirname + '/views/layouts'
}));
app.set('view engine', 'hbs');
app.use('/', routes);
app.use('/img', express.static('img'))
//Redirect to HTTPS
// app.use(function(req, res, next) {
// if ((req.get('X-Forwarded-Proto') !== 'https')) {
// res.redirect('https://' + req.get('Host') + req.url);
// } else
// next();
// });
// Starting both http & https servers
const httpServer = http.createServer(app);
const httpsServer = https.createServer(credentials, app);
httpServer.listen(80, () => {
console.log('HTTP Server running on port 80');
});
httpsServer.listen(443, () => {
console.log('HTTPS Server running on port 443');
});