By ggreenleaf
Hi,
I’ve built a MERN app and am using yarn as a package manager.
I’ve been able to get my backend running on my droplet using pm2 and it seems stable.
How do I then go about linking that running server.js process to my front-end?
I’ve purchased my domain on DreamHost and have the nameservers on DreamHost pointing to ns1.dreamhost.com , ns2.dreamhost.com , ns3.dreamhost.com and created the proper A records in my Droplet. I can go to the ip address where it’s being ran and I get a “Unauthorized: No access token found” error which makes sense as no user has logged in/is logged in.
I have my project folder that contains a ‘backend’ folder and ‘client’ folder on my Droplet. On my host machine during development, I’d simply cd into the backend and run yarn run dev and then cd into client and run yarn start to get up and running (client is localhost:3000 which has a proxy to localhost:5000 which the server is being ran on).
My initial thought was perhaps I yarn build in my client folder to get a production build and toss that on DreamHost as my frontend, and since I have the DNS pointing to DigitalOcean the api calls will run successfully??? I’m unsure as to how to go about this and there’s not that many resources online that go into this in-depth.
Thanks in advance for any help!
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi there,
What I could suggest is using a web service like Nginx to serve your frontend, that way you will also be able to install an SSL certificate as well.
Here’s how you can deploy your MERN stack app using Nginx:
Since you already have this running, ensure that your Node.js backend is listening on a local port, like localhost:5000.
Build the React App: On your local machine, navigate to the client directory and run:
yarn build
Upload the Build to DigitalOcean: Transfer the build directory to your DigitalOcean Droplet.
Install Nginx: If you haven’t already, install Nginx on your Droplet:
sudo apt update
sudo apt install nginx
Configure Nginx:
Open the Nginx default configuration:
sudo nano /etc/nginx/sites-available/default
Modify the configuration to serve your static files and proxy requests to your Node.js backend:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location / {
root /path/to/your/client/build;
try_files $uri /index.html;
}
location /api/ {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Make sure to replace /path/to/your/client/build with the actual path to the build directory on your Droplet. Also, if your backend routes don’t follow the /api/ pattern, adjust the location directive accordingly.
Next restart Nginx:
sudo systemctl restart nginx
After that configure your DNS:
Some additional remarks:
Environment Variables: Ensure all environment variables needed for production are set on your DigitalOcean Droplet.
Database: If your app uses a database, ensure it’s properly set up, optimized, and secured for production.
DreamHost: You’re primarily using DreamHost for domain registration and DNS management, since you’re serving both frontend and backend from the DigitalOcean Droplet using Nginx.
Best,
Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.