# Node.js driver + Atlas: one client, reused

Create the client once at startup and share it. A new MongoClient per request burns through your connection pool and on shared tiers you hit the connection cap fast.

```js
const { MongoClient } = require('mongodb');

const client = new MongoClient(process.env.MONGODB_URI, {
  maxPoolSize: 20,
  serverSelectionTimeoutMS: 5000,
});

async function start() {
  await client.connect();
  await client.db('admin').command({ ping: 1 });
  console.log('atlas reachable');
}
```

## Rules

- Put the Atlas SRV string (`mongodb+srv://...`) in an env var. Never hardcode it, never log it.
- `serverSelectionTimeoutMS: 5000` turns a hung connect into a fast, debuggable error instead of a 30s stall.
- `maxPoolSize` caps connections per client. 10 to 50 covers most apps; one client means one pool.
- The driver connects lazily, so an explicit `await client.connect()` plus a `ping` at startup proves the string, user, password, and IP access list entry all work before traffic arrives.
- Close with `await client.close()` only on process shutdown, never after a request.

## Verify

Run the startup ping from the same network your app runs on. If it fails, the problem is the connection string, the database user, or the IP access list, not your query code.