-1

I'm currently working on deploying a fullstack webapp to DigitalOcean, where I have 2 docker images - One for the frontend to statically serve files, and an ExpressJS backend that handles any logic for API calls. I have recently added CloudFlare to my website, which has enabled HTTPS, and while that is progress, now my backend is no longer responsive from the domain when doing HTTPS, if i access it by going to my droplet's ip(unsecure, HTTP) I can see that my api requests are working, but on the main HTTPS site, the entire backend is unresponsive.

I am currently mapping my frontend to port 80 outside of the docker container (from port 3000 inside) and 3001:3001 for my backend. I've heard that I should maybe use nginx, but I'm unsure how that works with my 2 docker images, and I've tried to use https.createServer(options, app) to try to get it to listen for the HTTPS requests, but to no avail.

Here is my current backend now:

const express = require("express");
const webserver = express();
const cors = require("cors");
const mysql = require("mysql");

// Connection pool here

webserver.use(cors());
webserver.use(express.json());

const PORT = 3001;

//api routes here

webserver.listen(PORT, () => {
  console.log(`[Express] Server running on ${PORT}`);
});

Any step in the right direction would be much appreciated, I've been researching how to fix this for ~10 hours with no luck.

Edit: I believe the solution is not to change the Express server to allow HTTPS, but somehow have Nginx do a reverse proxy and convert it to HTTP before it even hits the Express server, but I am unsure of how this is done.

Dylan
  • 25
  • 3
  • Does this answer your question? [Enabling HTTPS on express.js](https://stackoverflow.com/questions/11744975/enabling-https-on-express-js) – gre_gor Jan 27 '23 at 23:27
  • From that question: "For production systems, you're probably better off using Nginx or HAProxy to proxy requests to your nodejs app. You can set up nginx to handle the ssl requests and just speak http to your node app.js.". The problem with this is I can't find anything concrete to show how I would serve both of my dockerized containers to my knowledge – Dylan Jan 28 '23 at 05:58

1 Answers1

3

Instead of app.listen() or with your variable naming webserver.listen(), you have to manually create the server instance using https.createServer() and pass it the security credentials for your https server:

const https = require('https');
const express = require("express");
const app = express();

const server = https.createServer({
      key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
      cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem'),
}, app);

server.listen(443);

Relevant https doc here and Express doc here.

jfriend00
  • 683,504
  • 96
  • 985
  • 979