forked from battlejj/express-force-ssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
34 lines (28 loc) · 924 Bytes
/
example.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
var express = require('express')
, forceSSL = require('./../index')
, fs = require('fs')
, http = require('http')
, https = require('https')
;
var ssl_options = {
key: fs.readFileSync('./test/keys/localhost.key'),
cert: fs.readFileSync('./test/keys/localhost.crt'),
ca: fs.readFileSync('./test/keys/localhost.crt')
};
var app = express();
var server = http.createServer(app);
var secureServer = https.createServer(ssl_options, app);
app.get('/', function(req, res){
res.json({msg: 'accessible by http'});
});
app.get('/ssl', forceSSL, function(req, res){
res.json({msg: 'only https'});
});
app.get('/ssl/deep/route/:id', forceSSL, function(req, res){
var host = req.headers.host.split(':');
var port = host.length > 1 ? host[1] : 'default port';
res.json({msg: 'only https, port: ' + port, id: req.param('id')});
});
app.set('httpsPort', 8443);
secureServer.listen(8443);
server.listen(8080);