I want to host a React app on my Cloudflare domain and manage SQL requests using my Node.js web server running on localhost:3500. Both the React app and the API are running smoothly. I used Certbot to secure the React app with an HTTPS certificate. I also set up Cloudflare DNS records for both the root domain and the www subdomain.
Currently, my React app runs over HTTPS, while the Node.js API runs over HTTP. I attempted to upgrade my Node.js server to HTTPS by modifying server.js and adding a certificate, but it didn’t work because the server was flagged as “not secure.” I used the same certificate from Certbot that I used for the React app.
My file structure:
/var/www/html
- build (my react app)
- api (server.js nodejs api)
- phpmyadmin (mysql management)
Here’s my nginx conf:
(I replaced mydomain and server ip:3500 in my code)
upstream loadbalancer {
least_conn;
server ip:3500;
}
server {
listen 80;
server_name mydomain.com www.mydomain.com;
# React app & front-end files
location / {
root /var/www/html/build;
try_files $uri /index.html;
}
location /api/ {
proxy_pass http://loadbalancer/;
proxy_buffering on;
}
}
For my NodeJS WebServer i have a process.yml via pm2
apps:
- script: './api/server.js'
exec_mode: 'fork'
name: 'worker-0'
env:
PORT: 3500
NODE_ENV: production
My server.js:
I removed the sql routes
const express = require('express');
const mysql = require('mysql');
const cors = require('cors');
const http = require('http');
const app = express();
app.use(cors());
app.use(express.json());
const port = process.env.PORT || 3500;
var httpServer = http.createServer(app);
httpServer.listen(port, () => {
console.log("Running HTTP Server on ", port);
});
My react app sends https requests to my nodejs api webserver but obviously that wont work.
What can i do to solve that problem?