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:
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.