Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERR_SSL_PROTOCOL_ERROR browser error message while running a https server in nodejs express

I have a number of nodejs applications running on Express using the code below. They all run fine using code similar to the following:

fs = require 'fs'                                                               
https = require 'https'                                                         
express = require 'express'                                                     
server_port = 3000                                                              
keys_dir = 'keys/'                                                              server_options = {
  key  : fs.readFileSync(keys_dir + 'privatekey.pem'), 
  cert : fs.readFileSync(keys_dir + 'certificate.pem')                          }                                                                                             app = express.createServer(server_options)
app.listen server_port 
console.log "HTTPS Server started on port #{server_port}"  

However, when trying to create a new application using this code I see a ERR_SSL_PROTOCOL_ERROR when starting the https server. Any idea what is causing this problem?

like image 729
SnapShot Avatar asked Sep 09 '26 02:09

SnapShot


1 Answers

I discovered that was caused when moving from express 2.5.8 to express 3 - specifically 3.0.0beta4. When creating a new project the version pulled from npm had changed to the version 3 series. Even though the module is marked as "beta" when you run express --version this version is what is installed now when running npm install express. The details of the changes are outlined here.

To solve this for my case I used the following code:

const fs = require("fs");
const https = require("https");
const express = require("express");

const keysDir = "keys/";
const options = {
  key  : fs.readFileSync(keysDir + "privatekey.pem"),
  ca   : fs.readFileSync(keysDir + "certrequest.csr"),
  cert : fs.readFileSync(keysDir + "certificate.pem")
};

const app = express();
https.createServer(options, app).listen(3000);
like image 121
SnapShot Avatar answered Sep 11 '26 19:09

SnapShot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!