skip to Main Content

I am trying to get https working on my nodejs server like so:

var http = require('http');
var https = require('https');
var fs = require('fs');
var express = require('express');
var privateKey  = fs.readFileSync('server.key', 'utf8');
var certificate = fs.readFileSync('server.crt', 'utf8');

var credentials = {key: privateKey, cert: certificate};
var app = express();

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);

httpServer.listen(8080, ()=> {
    console.log('Server started and listening on port 8080...')
});
httpsServer.listen(8443, ()=>{
    console.log('Server started and listening on port 8443...')
});

When I run the server only the http url is working, the https gets timed out.
any idea why this is happening?

I am used to work with cpanel, so I setup the ssl cert on there already, but when it comes to node, I am hitting a wall.

2

Answers


  1. Chosen as BEST ANSWER

    Problem was the port wasn't forwarded.


  2. It seems like there’s something wrong with your ssl files, You need to give certificate file and private key while creating https server like this:

    var express = require('express');
    var https = require('https');
    var http = require('http');
    var fs = require('fs');
    
    // This line is from the Node.js HTTPS documentation.
    var options = {
      key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
      cert: fs.readFileSync('test/fixtures/keys/agent2-cert.cert')
    };
    
    // Create a service (the app object is just a callback).
    var app = express();
    
    // Create an HTTP service.
    http.createServer(app).listen(80);
    // Create an HTTPS service identical to the HTTP service.
    https.createServer(options, app).listen(443);
    

    Hope it helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search