Created
June 8, 2012 12:50
-
-
Save doomhz/2895441 to your computer and use it in GitHub Desktop.
SSL with Node and Express
This file contains 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
// SSL with Node and Express method 1: | |
var express = require('express') | |
, fs = require("fs") | |
, routes = require('./routes'); | |
var privateKey = fs.readFileSync('cert/key.pem').toString(); | |
var certificate = fs.readFileSync('cert/certificate.pem').toString(); | |
// to enable https | |
var app = module.exports = express.createServer({key: privateKey, cert: certificate}); | |
// SSL with Node and Express method 2: | |
var restify = require('restify'); | |
var fs = require('fs'); | |
// Setup some https server options | |
var https_options = { | |
key: fs.readFileSync('/etc/ssl/self-signed/server.key'), | |
certificate: fs.readFileSync('/etc/ssl/self-signed/server.crt') | |
}; | |
// Instantiate our two servers | |
var server = restify.createServer(); | |
var https_server = restify.createServer(https_options); | |
// Put any routing, response, etc. logic here. This allows us to define these functions | |
// only once, and it will be re-used on both the HTTP and HTTPs servers | |
var setup_server = function(app) { | |
function respond(req, res, next) { | |
res.send('I see you ' + req.params.name); | |
} | |
// Routes | |
app.get('/test/:name', respond); | |
} | |
// Now, setup both servers in one step | |
setup_server(server); | |
setup_server(https_server); | |
// Start our servers to listen on the appropriate ports | |
server.listen(80, function() { | |
console.log('%s listening at %s', server.name, server.url); | |
}); | |
https_server.listen(443, function() { | |
console.log('%s listening at %s', https_server.name, https_server.url); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment