0

Perhaps I'm not understanding how SSL/HTTPS works (likely), but SalesForce's API requires an https connection in their callback so I'm trying to get my head around it.

Here's how I've set it up in app.js

var http        = require('http');
var https       = require('https');
var privateKey  = fs.readFileSync('private/key.pem', 'utf8');
var certificate = fs.readFileSync('private/cert.pem', 'utf8');
var credentials = {key: privateKey, cert: certificate};



var port        = process.env.PORT || 8080;

http.createServer(app).listen(8081);
https.createServer(credentials, app).listen(port);

I'm accessing my local dev environment with localhost:8080, localhost:8080/profile etc.

When I type this into the address bar, I get this error:

http://i.imgur.com/Hlz0dR5.png

Instead, I have to explicitly type "https://" in front of it, then it works.

From looking around SO I've seen some similar questions which suggesting using the middleware:

app.use(function(req, res, next) {
    console.log(req.secure);
  if(!req.secure) {
    var url = ['https://', req.get('Host'), req.url].join('')
    console.log(url);

    return res.redirect(url);
  }
  next();
});

However, this doesn't work; if I go to localhost:8080 I'm not redirected anywhere (I get the same error as above), and if I got to localhost:8081 I get redirected to https://localhost:8081 which obviously doesn't work because we need to be on port 8080 for HTTPS.

Any ideas on what I'm doing wrong here? I'd just like to run the whole thing on HTTPS by default.

JVG
  • 20,198
  • 47
  • 132
  • 210
  • Is it required to do so using express? Its standard practice to use nginx for ssl termination and as a reverse proxy. – Swaraj Giri Jul 10 '15 at 10:07
  • Do you have any examples you can show? I'm doing this on localhost so not using nginx on OSX – JVG Jul 10 '15 at 12:15
  • Not an OSX user, but something on the lines of http://brianflove.com/2014/12/01/self-signed-ssl-certificate-on-mac-yosemite/ should work. – Swaraj Giri Jul 12 '15 at 03:47

0 Answers0