Connecting a Node.js application to MongoDB
This guide has been reviewed and reformatted for Ruk-Com Cloud PaaS. Screens may vary slightly by platform version.
Click or tap a screenshot to view it at its original size.
Objective
This guide explains how to use Connecting a Node.js application to MongoDB on Ruk-Com Cloud PaaS, with ordered procedures and practical verification points.
Before you begin
- Sign in with an account permitted to manage the relevant environment.
- Confirm the target environment, region and resources before saving changes.
- Create a backup or rollback plan before changing a production system.
MongoDBIt is a popular NoSQL database that Ruk-Com Cloud supports and can be easily deployed on the cloud. Below we will consider a simple example to connect to a DB stack from an application server.Node.js
1. To follow this guide, you will need a Node.js and MongoDB server within Ruk-Com Cloud (you cancreateat any time) or on other external resources.

In our case, both instances are hosted in the same environment.
2. Connect to your application server via Ruk-Com SSH Gate.

3. Then download and install.MongoDB driver for Node.js
npm install mongodb

The package will install successfully soon.
4. Now create a file with the script to establish a connection to your database. Your preferred text editor can be used for this task, as well as filenames with the extension.js (e.g. vim script.js).
var MongoClient = require('mongodb').MongoClient;
// Connect to the db
MongoClient.connect("mongodb://{user}:{password}@{host}:{port}/{database}", { useUnifiedTopology: true, useNewUrlParser: true }, function(err, db) {
if(!err) {
console.log("You are connected!");
};
db.close();
});
adjustconnection string(All required information is in the email for MongoDB node )
- {user}- Username for logging into the database
- {password}- Password for appropriate user
- {host}- Connect to MongoDB container.
- {port}- Port used for connection (The default value is 27017)
- {database}- The database you want to access (e.g. admin which is the default)

With this script you can access the specified database server and if the connection is established successfully. You will see the message “You are connected!” displayed.
5. Run the code using the appropriate command.
node script.js

If everything is specified correctly, you will see the message “You are connected!” within the terminal. Next you can write additional code to perform all the required actions.