Http and https (or just https) for Nodejs app on specific port

I am using Apache and Ubuntu 18.04 on AWS and managing my domain using Route53. I am running a node js application on my server using port 3001. The main website is on the main domain and the application interface is running on
3 certificates are installed, one for and, another one for, and the third one which I just installed it is a wildcard *
Right now there is no issue with the mail domain and the subdomain on https and my app is working fine on however I need to run it on https as well, so I want to keep the app running on both http and https, but if it’s not possible, I want to keep it running on https instead of http.
I stuck on how to handle it.

The version of my client is 1.3.0.

I guess you are probably doing something like this:

const http = require('http');

const httpServer = http.createServer(expressApp);

You can pretty much substitute it with something like:

const https = require('https');
const fs = require('fs');

const httpsServer = https.createServer({
  key: fs.readFileSync('/etc/letsencrypt/live/'),
  cert: fs.readFileSync('/etc/letsencrypt/live/'),
}, expressApp);


You might also need to need to allow your Node server’s user to read the certificate and private key, as by default they are not world-readable.

If you want, you could move the non-secure version to another port like 3002. AFAIK Node.js can’t mux HTTP and HTTPS on one port.

1 Like

This is fixed by adding another subdomain and removing the port number, so now we have both http and https without any problem.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.