1

I have a Node.js server, with an active SSL certificate on the domain. I have read some replies on this website about this, yet even when I relate to such already-solved questions, I get an error.

var express = require('express');
var https = require('https');
var http = require('http');
var path = require('path');
var fs = require('fs');
var mysql = require('mysql');

var queue = {};
var qc = {};

var app = express();

var options = {
    key: fs.readFileSync('sslcert/domain-name.key', 'utf8'),
    cert: fs.readFileSync('sslcert/domain-name.csr', 'utf8')
};

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

/* various stuff to display my website */

httpServer.listen(process.env.PORT);
httpsServer.listen(process.env.PORT);

I get the following error in my console.

_tls_common.js:67
      c.context.setCert(options.cert);
                ^

Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
    at Error (native)
    at Object.createSecureContext (_tls_common.js:67:17)
    at Server (_tls_wrap.js:754:25)
    at new Server (https.js:17:14)
    at Object.exports.createServer (https.js:37:10)
    at Object.<anonymous> (/srv/data/web/vhosts/default/server.js:35:25)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)

Thank you in advance for your answer!

Noël.

Noël Nadal
  • 35
  • 1
  • 7
  • Possible duplicate of [OpenSSL: PEM routines:PEM\_read\_bio:no start line:pem\_lib.c:703:Expecting: TRUSTED CERTIFICATE](http://stackoverflow.com/questions/20837161/openssl-pem-routinespem-read-biono-start-linepem-lib-c703expecting-truste) – Ortomala Lokni Apr 03 '17 at 15:14

2 Answers2

1

cert should be your domain's PEM formatted certificate, followed by the PEM formatted intermediate certificates; not the CSR.

cert: fs.readFileSync('sslcert/domain-name.pem', 'utf8')

Anand Bhat
  • 5,591
  • 26
  • 30
1

In addition to the answer by Anand Bhat note that you shouldn't bind both of those servers to the same port like you're trying to do:

httpServer.listen(process.env.PORT);
httpsServer.listen(process.env.PORT);

It might work but it might work not how you expect it. If you do:

httpServer.listen(process.env.PORT_HTTP);
httpsServer.listen(process.env.PORT_HTTPS);

where PORT_HTTP is e.g. 80 and PORT_HTTPS is e.g. 443, you will always know which server will process which request.

rsp
  • 107,747
  • 29
  • 201
  • 177