Updated on 2026-08-04 GMT+08:00

Node.js

Scenarios

DDS allows you to perform data operations using Node.js. You can connect to a DB instance through an SSL connection or a non-SSL connection. SSL connections provide stronger security.

By default, SSL is disabled for new DDS instances. For details about how to enable SSL, see Configuring SSL.

This section describes how to connect to a cluster instance using Node.js.

Prerequisites

  1. Ensure that the ECS can communicate with the DDS instance. To test the connectivity to the instance's IP address and port, run the curl command:

    curl ip:port

    If the message "It looks like you are trying to access MongoDB over HTTP on the native driver port." is displayed, they can communicate with each other.

  2. If SSL is enabled, you must download the root certificate and upload it to the ECS.

Configuring the Environment and Obtaining the Driver

  1. Ensure that Node.js is installed in your environment. For download information, see the official Node.js website.
  2. Initialize the project. Specifically, create a project directory, navigate into it, and then run the initialization command to generate the package.json file:
    mkdir your_project
    cd your_project
    npm init -y
  3. Install the driver.
    npm install mongodb@3.7.4

Enabling and Disabling SSL

In the your_project directory, create a file named index.js for the application and copy the sample code into the file.

const { MongoClient } = require('mongodb');
const uri = 'mongodb://rwuser:rwuserpassword@ip:port/{mydb}?authSource=admin';
const client = new MongoClient(uri, {
    serverSelectionTimeoutMS: 5000, // If no available node is found within 5 seconds, an error is reported.
    connectTimeoutMS: 5000,         // A single TCP connection times out after 5 seconds.
    socketTimeoutMS: 10000          // The socket times out after being idle for 10 seconds.
});
async function main() {
    try {
        await client.connect();
        console.log('Successfully connected to MongoDB');
        // Connectivity test
        const result = await client.db('admin').command({ ping: 1 });
        console.log('Ping result:', result);
        // Example: Read data.
        const db = client.db('test');
        const collection = db.collection('users');
        const doc = await collection.findOne({});
        console.log('First document found:', doc);
    } catch (err) {
        console.error('Connection or operation failed:', err);
    } finally {
        await client.close();
    }
}
main().catch(console.dir);
const { MongoClient } = require('mongodb');
const uri = 'mongodb://rwuser:rwuserpassword@ip:port/{mydb}?authSource=admin';
const client = new MongoClient(uri, {
    serverSelectionTimeoutMS: 5000, // If no available node is found within 5 seconds, an error is reported.
    connectTimeoutMS: 5000,         // A single TCP connection times out after 5 seconds.
    socketTimeoutMS: 10000,        // The socket times out after being idle for 10 seconds.
    tls: true,
    tlsCAFile:'./ca.crt',           // Certificate address
    tlsAllowInvalidHostnames: true
});
async function main() {
    try {
        await client.connect();
        console.log('Successfully connected to MongoDB');
        // Connectivity test
        const result = await client.db('admin').command({ ping: 1 });
        console.log('Ping result:', result);
        // Example: Read data.
        const db = client.db('test');
        const collection = db.collection('users');
        const doc = await collection.findOne({});
        console.log('First document found:', doc);
    } catch (err) {
        console.error('Connection or operation failed:', err);
    } finally {
        await client.close();
    }
}
main().catch(console.dir);