How to Open Database Connections in a Node.js/Express App

Konstantin Tarkus
1 min readOct 4, 2015

I believe that most developers working with databases already know what is a connection pool — a cache of database connections maintained so that the connections can be reused when future requests to the database are required. Connection pools are used to enhance the performance of executing commands on a database. This is a standard feature of many database driver libraries but yet is often neglected by web developers.

Below you can find a couple of examples, one for MongoDB and another one for Azure SQL Database, showing how to open database connections by using a connection pool.

Example 1: Azure SQL Database

/api/users.js

/config.js

/server.js

Example 2: MongoDB Native Driver

/api/users.js

/config.js

/server.js

--

--

Konstantin Tarkus

Empowering startups with cutting-edge expertise: software architecture, optimal practices, database design, web infrastructure, and DevOps mastery.