I created two droplets, deployed my Nodejs backed in first one, installed Mongodb in the second one. I also secured the MongoDB with database admin user, and a SSH key login to the MongoDB Droplet. Now I am stuck, I don’t know how to connect my Nodejs app to MongoDB Database. How will I connect to the database? Using a Connection String? or by attaching and sending an object with all port values, ssh key etc? I am not sure how to get it done, any help will be highly appreciated. Thank you.
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!
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.
By default, the MongoDB listens on the local interface. In order to allow your Node application to access the MongoDB instance, modify the value of
/etc/mongod.confIf you do so, you are highly advised to first review the security checklist from the MongoDB documentation.
The relevant section of the config file should look like:
In addition to enabling one of the forms of authentication supported by MongoDB, you should set up a firewall to limit access to the MongoDB instance. You can do this with a DigitalOcean firewall or UFW which comes pre-installed on Ubuntu Droplets.
If you go with UFW, this command will allow access from the Node app’s IP address and nowhere else:
Now that MongoDB is listening for connections from the outside and has been secured with a firewall, you can connect to it from Node using the MongoDB Node.JS driver
In order to make your app more portable, consider making the value of
urlin the above example configurable using an environment variable.