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 https://getgoldoon.com and the application interface is running on https://app.getgoldoon.com.
3 certificates are installed, one for getgoldoon.com and www.getgoldoon.com, another one for app.getgoldoon.com, and the third one which I just installed it is a wildcard *.getgoldoon.com
Right now there is no issue with the mail domain and the subdomain on https and my app is working fine on http://app.getgoldoon.com:3001 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);
httpServer.listen(3001);

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/app.getgoldoon.com/privkey.pem'),
  cert: fs.readFileSync('/etc/letsencrypt/live/app.getgoldoon.com/fullchain.pem'),
}, expressApp);

httpsServer.listen(3001);

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.