Last active
August 29, 2015 14:19
-
-
Save CaptainYarb/586d262eb16ccde5a36a to your computer and use it in GitHub Desktop.
Secure Socket.io + Express
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var io = require('socket.io'), | |
express = require('express'), | |
http = require('http'), | |
https = require('https'), | |
fs = require('fs'), | |
splitca = require('split-ca'); | |
module.exports = function(){ | |
var certs = { | |
key: fs.readFileSync('path/to/cert.key'), | |
cert: fs.readFileSync('path/to/cert.crt'), | |
ca: splitca('path/to/cert.ca'), | |
requestCert: true | |
}; | |
var expressApp = express(), | |
httpServer = http.createServer(expressApp), | |
secureServer = https.createServer(certs, expressApp), | |
server = io(secureServer); | |
expressApp.get('*', function(req, res){ | |
if(req.secure){ | |
return res.send('Secure!'); | |
}else{ | |
return res.redirect("https://" + req.headers.host + req.url); | |
} | |
}); | |
secureServer.listen(443); | |
httpServer.listen(80); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment