Arjun Patel
Arjun Patel

Reputation: 276

SSL with express.js trouble

I'm trying to add ssl certificates to my node server but my webpage continues to load in http and not https. I was wondering if someone can point me in the right direction. Kind of pulling my hair out lol. Here is a snippet of the some of the important code:

var options = {
  key: fs.readFileSync('/home/ec2-user/key.pem'),
  cert: fs.readFileSync('/home/ec2-user/cert.pem'),
  ca: fs.readFileSync('/home/ec2-user/ca.pem'),
};

var app = express();
app.set('root', __dirname);

https.createServer(options, app);
app.listen(APP_PORT, APP_HOST);

// Listener endpoint; handled in middleware
app.get('/listen', function(){});

Upvotes: 4

Views: 2861

Answers (1)

hexacyanide
hexacyanide

Reputation: 91649

You are setting your Express application to listen instead of your HTTPS server. Use this instead:

var https = require('https');
var express = require('express');
var app = express();

var options = {
  key: fs.readFileSync('/home/ec2-user/key.pem'),
  cert: fs.readFileSync('/home/ec2-user/cert.pem'),
  ca: fs.readFileSync('/home/ec2-user/ca.pem')
};

app.set('root', __dirname);
app.get('/listen', function(req, res) {
  // respond here
});

var server = https.createServer(options, app);
server.listen(APP_PORT, APP_HOST);

You don't need to specify an IP for the application if you don't have multiple addresses on your machine.

Upvotes: 4

Related Questions