Skip to content

Instantly share code, notes, and snippets.

@lydonchandra
Created August 12, 2012 20:00
Show Gist options
  • Save lydonchandra/3334083 to your computer and use it in GitHub Desktop.
Save lydonchandra/3334083 to your computer and use it in GitHub Desktop.
// HTTPS in ExpressJS 3.0
// Exec these 3 lines to generate your PEM files (Linux, OSX with OpenSSL installed)
//> openssl genrsa -out privatekey.pem 1024
//> openssl req -new -key privatekey.pem -out certrequest.csr
//> openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem
//Express app.js, HTTPS on port 3443
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, http = require('http')
, https = require('https')
, fs = require('fs')
, path = require('path');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
app.get('/api/thumbs', routes.api.thumbs);
var privateKey = fs.readFileSync('privatekey.pem').toString();
var certificate = fs.readFileSync('certificate.pem').toString();
var certrequest = fs.readFileSync('certrequest.csr').toString();
var credentials = { key: privateKey,
cert: certificate,
ca: certrequest };
var httpsServer = https.createServer(
credentials ,
app );
httpsServer.listen(3443, function(){
console.log("Express server listening on port " + 3443);
});
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment