I have a Node/Express server running on an AWS Lightsail instance with PM2 as a process manager. The server is currently listening on port 4000. The IP address for the instance is attached to a subdomain that has a valid SSL certificate and automatically redirects from HTTP to HTTPS. Visiting https://example.com at the moment shows the 'Congratulations! You are now running Bitnami Node.js 12.18.3 in the Cloud." page.
Currently, all the Express endpoints are only accessible through http://example.com:4000/endpoint, but I want the Express app to run on port 443 so that the endpoints are accessible immediately on https://example.com/endpoint.
I read that PM2 is able to listen on ports 80 and 443 and tried the method mentioned in the documentation, but whenever I change the port number in the .env file to 443 and reload the app using pm2 reload app
, I get the following error:
0|app | Error: listen EADDRINUSE: address already in use :::443
0|app | at Server.setupListenHandle [as _listen2] (net.js:1313:16)
0|app | at listenInCluster (net.js:1361:12)
0|app | at Server.listen (net.js:1447:7)
0|app | at Function.listen (/opt/bitnami/apache/htdocs/node_modules/express/lib/application.js:618:24)
0|app | at Object.<anonymous> (/opt/bitnami/apache/htdocs/app.js:44:5)
0|app | at Module._compile (internal/modules/cjs/loader.js:1137:30)
0|app | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
0|app | at Module.load (internal/modules/cjs/loader.js:985:32)
0|app | at Function.Module._load (internal/modules/cjs/loader.js:878:14)
0|app | at Object.<anonymous> (/opt/bitnami/node/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23) {
0|app | code: 'EADDRINUSE',
0|app | errno: 'EADDRINUSE',
0|app | syscall: 'listen',
0|app | address: '::',
0|app | port: 443
0|app | }
App.js
const express = require('express');
const dotenv = require('dotenv');
const app = express();
app.use(express.json()); // for parsing POST bodies
dotenv.config();
app.get("/hello", (req, res) => res.send("Hello World!"));
app.listen(process.env.PORT, () => {
console.log(`🥁 App listening on port ${process.env.PORT}!`);
});
.env
PORT=443
Edit: pm2 status output
Unfortunately I wasn't able to use Nginx, but instead used Apache's virtual hosts config to redirect all traffic from ports 443 to 4000. More info here.