Created
January 27, 2013 10:35
-
-
Save dearfrankg/4647782 to your computer and use it in GitHub Desktop.
working https
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
var express = require('express'), | |
http = require('http'), | |
https = require('https'), | |
fs = require('fs'); | |
var app = express(); | |
// Configuration | |
// | |
app.configure(function(){ | |
app.set('port', process.env.PORT || 8000); | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(app.router); | |
}); | |
app.configure('development', function(){ | |
app.use(express.errorHandler( | |
{ dumpExceptions: true, showStack: true })); | |
}); | |
app.configure('production', function(){ | |
app.use(express.errorHandler()); | |
}); | |
// Routing | |
// | |
app.get('/', function(req,res) { | |
//console.log(req.ssl); | |
if (req.ssl) { | |
res.end("hello https"); | |
} else { | |
res.end("hello http"); | |
} | |
}); | |
// Start server listening | |
// | |
var httpsOptions = { | |
key: fs.readFileSync('./ssl/server.key'), | |
cert: fs.readFileSync('./ssl/server.crt'), | |
ca: fs.readFileSync('./ssl/ca.crt'), | |
requestCert: false, | |
rejectUnauthorized: false, | |
passphrase: 'frankg' | |
}; | |
http.createServer(app).listen( app.get('port') ); // HTTP | |
var apps = https.createServer(httpsOptions, app).listen(443); // HTTPS | |
apps.addListener( "request", function sslRequestListener( req, res ) { | |
req.ssl = true; // see if it was forwarded from https | |
//console.log('req.ssl', req.ssl); | |
// app.emit( "request", req, res ); // app is my express instance | |
}); | |
// Launch message | |
// | |
console.log("Express server listening on port %s in %s mode", app.get('port'), app.settings.env); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment