Question

How to setup an application (MERN) in a single Droplet

Hi community,

I have created a droplet Ubuntu 20.04 (LTS) x64. Inside this server I have configured a MongoDB database (version 4.4), a NodeJS (version 14.18.3) and inside my NodeJS enviroment I have installed a frontend application (React) and a backend (Express).

I have the following status:

  • Frontend: is accessible from the Browser (no issue so far), meaning that the installation/configuration is successfull;
  • Backend: is accessible from the Browser. I have created a route “/ping” and the answer is “pong” as expected;
  • MongodB: Works ok from the console. It is up and running and answering from the console

The issue: When I try to access MongodB through the backend “http://(iphost}:3333/login”, the backend stops working (Error: socket hang up) meaning that I might have an issue between the backend and MongodB. In more details it shows an error related to decryption (Error: Error during decryption (probably incorrect key). Original error: Error: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error), however my “.pem” file is correct and the address of this file is also correct.

My question: Is there any kind of limitation in terms of PORTS access that might avoid this communication between Backend (3333) and MongodB (27017)? Has anyone faced this kind of error? Any suggestion?

Important: This application (frontend+backend+mongodB) has the same configuration as I have installed and is working in my local computer.

Important(2): Unfortunatelly I cannot upgrade my system (mongodb, NodeJs and NPM) because it is a legacy application.

Many thanks and Regards


Submit an answer


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!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
December 21, 2023

Hey Marcos,

Happy to hear that you’ve made such a good progress. Here are a few steps that you can follow to help you with further troubleshooting this:

  1. Review Firewall Settings: Start by reviewing your firewall settings. Ensure that your firewall isn’t blocking MongoDB’s default port (27017). You can check the active firewall rules using:

    sudo ufw status
    

    If necessary, allow traffic on MongoDB’s port:

    sudo ufw allow 27017
    
  2. Confirm MongoDB Configuration: If your MongoDB instance is running on your Droplet as well, verify MongoDB’s mongod.conf file to ensure it’s correctly set up to listen on the appropriate network interface. The configuration file is typically located at /etc/mongod.conf. You should see a line like bindIp: 127.0.0.1 (for localhost connections) or bindIp: 0.0.0.0 (to listen on all interfaces).

  3. Testing MongoDB Connection: Test the MongoDB connection independently from your backend. Use the mongosh command:

    mongosh "mongodb://localhost:27017"
    

    This helps confirm that MongoDB is running and accessible.

  4. Backend Service Debugging: Start your Node.js backend and watch for errors. If it crashes after trying to connect to MongoDB, the console should provide some insight. You can start your Node.js app with:

    node app.js
    

    Replace app.js with your main server file.

  5. Review Node.js MongoDB Connection String: In your Node.js application, ensure the MongoDB connection string is correctly formatted. It generally looks like this:

    const mongoose = require('mongoose');
    mongoose.connect('mongodb://localhost:27017/yourdbname', { useNewUrlParser: true, useUnifiedTopology: true });
    
  6. Inspect SSL/TLS Configuration: Since you mentioned a decryption error, double-check your SSL/TLS configuration for MongoDB. Ensure the .pem file path is correct and the file is accessible by your Node.js app.

  7. Nginx Reverse Proxy Setup: For Nginx, you’ll want to set up reverse proxy settings for your Node.js app. Here’s an example configuration snippet for your /etc/nginx/sites-available/default file:

    server {
        listen 80;
        server_name your_domain_or_IP;
    
        location / {
            proxy_pass http://localhost:3333; # Replace with your Node.js port
            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;
        }
    }
    

    After updating, restart Nginx:

    sudo systemctl restart nginx
    
  8. Logging and Monitoring: Keep an eye on both Nginx and Node.js logs for any errors that might provide more clues:

    • Nginx: /var/log/nginx/error.log
    • Node.js: You can add custom logging within your application or use a process manager like PM2 that keeps logs.

If you are using a managed MongoDB cluster, you would need to make sure that your backend Droplet IP address is allowed in the trusted sources of the MongoDB cluster.

Keep us posted on your progress, and don’t hesitate to reach out for further assistance!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel