Skip to content

Instantly share code, notes, and snippets.

@3rd-Eden
Created April 5, 2011 13:35
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save 3rd-Eden/903596 to your computer and use it in GitHub Desktop.
Save 3rd-Eden/903596 to your computer and use it in GitHub Desktop.
// Build the https based part & read the key and crt file that is required to encrypte the server / client connection
var ssl = https.createServer({
key: fs.readFileSync( "./ssl/ssl.private.key" ).toString()
, cert: fs.readFileSync( "./ssl/ssl.crt" ).toString()
});
// This is the part what it's all about, we are going to route all
// https based requires to the default app handler
ssl.addListener( "request", function sllRequestListener( req, res ){
req.ssl = true; // just add an extra flag so we can see if it was forwarded from https
app.emit( "request", req, res ); // app is my express instance
});
@mbabuskov
Copy link

I cannot get this to work. It reports Can't set headers after they are sent and node dies:

http.js:536
    throw new Error("Can't set headers after they are sent.");
          ^
Error: Can't set headers after they are sent.
    at ServerResponse.<anonymous> (http.js:536:11)
    at ServerResponse.setHeader (/root/ssl/node_modules/express/node_modules/connect/lib/patch.js:62:20)
    at next (/root/ssl/node_modules/express/node_modules/connect/lib/http.js:171:13)
    at HTTPServer.handle (/root/ssl/node_modules/express/node_modules/connect/lib/http.js:217:3)
    at HTTPServer.emit (events.js:70:17)
    at HTTPSServer.<anonymous> (/root/ssl/ssl4.js:33:7)
    at HTTPSServer.emit (events.js:88:20)
    at HTTPParser.onIncoming (http.js:1610:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:91:29)
    at CleartextStream.ondata (http.js:1506:22)

@3rd-Eden
Copy link
Author

3rd-Eden commented Jun 5, 2012

Looks to me that you are trying to handle a request hat is already handled.

@mbabuskov
Copy link

Here's my complete code:

var sys = require('util'),
express = require('express'),
fs = require('fs'),
connect = require('connect')
;
var pkey = fs.readFileSync('oated.key').toString();
var cert = fs.readFileSync('oated.com.crt').toString();
var dad1 = fs.readFileSync('gd_bundle1.crt').toString();
var dad2 = fs.readFileSync('gd_bundle2.crt').toString();
var apps = express.createServer({key: pkey, cert: cert, ca: [dad1,dad2]});
var app = express.createServer();
apps.listen(443);
app.listen(80);

app.configure(function () {
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(connect.static('public'));
});

app.get('/', function(req, res){
if (req.ssl)
res.end('Hello HTTPS');
else
res.end('Hello HTTP');
});

apps.addListener( "request", function sslRequestListener( req, res ){
req.ssl = true; // just add an extra flag so we can see if it was forwarded from https
app.emit( "request", req, res ); // app is my express instance
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment