I'm learning how to use HTTPS on Express, using as example, a certificate that I generated at StartSSL. I tried taking a look at other solutions over there, but nothing seems to work for me.
Ok, so I generated a .pem file in StartSSL, with a key I saved here. I've put the .pem file in the Express app folder, and I'm trying to set up a working server with this code:
const app = require('express')();
const fs = require('fs');
const https = require('https');
// Setup HTTPS
const httpsPort = 3443;
const options = {
key: "my key in plain text",
cert: fs.readFileSync("./cert.pem")
};
var secureServer = https.createServer(options, app).listen(httpsPort, () => {
console.log(">> App listening at port "+httpsPort);
});
I also tried with .crt certificate and so, but nothing seems to work. It throws the following error:
C:\some\path\test>node index.js
_tls_common.js:85
c.context.setKey(options.key, options.passphrase);
^
Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
at Error (native)
at Object.createSecureContext (_tls_common.js:85:17)
at Server (_tls_wrap.js:776:25)
at new Server (https.js:26:14)
at Object.exports.createServer (https.js:47:10)
at Object.<anonymous> (C:\some\path\test\index.js:18:26)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
What am I doing wrong, guys? Thank you!
EDIT:
Also tried putting the password in a .pem file with the following content:
-----BEGIN PRIVATE KEY-----
(my key)
-----END PRIVATE KEY-----
Which throws another error, maybe more fs-related:
Error: error:0906D064:PEM routines:PEM_read_bio:bad base64 decode