-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
61 lines (44 loc) · 1.33 KB
/
test.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
var express = require('express');
var app = express();
var MongoStore = require('connect-mongo')(express);
var fs = require('fs');
var http = require('http');
var https = require('https');
var fs = require('fs');
var sslOptions = {
key: fs.readFileSync('../server.key'),
cert: fs.readFileSync('../server.crt'),
ca: fs.readFileSync('../ca.crt'),
requestCert: true,
rejectUnauthorized: false
};
function requireHTTPS(req, res, next) {
if (!req.secure) {
//FYI this should work for local development as well
//return res.redirect('https://' + req.get('host') + req.url);
return res.redirect('https://localhost:3465' + req.url);
}
next();
}
app.use(express.cookieParser());
app.use(express.session({
secret: '1234567890QWERTY',
store: new MongoStore({
db: 'users'
})
}));
app.use(requireHTTPS);
app.get('/', function(req, res) {
if(req.session.visits === undefined) {
req.session.visits = 0;
}
else {
req.session.visits++;
}
res.send('You have been here ' + req.session.visits + ' times before.');
});
http.createServer(app).listen(3000);
https.createServer(sslOptions,app).listen(3465, function(){
console.log("Secure Express server listening on port 3465");
console.log("Redirect server running on port 3000");
});