Skip to main content

ExpressJS Database Integration

ExpressJS Database Integration

ExpressJS is a versatile web framework for Node.js that allows developers to easily build web applications and APIs. Integrating databases with ExpressJS enables applications to store and retrieve data efficiently. In this guide, we'll explore how to integrate popular databases like MongoDB, MySQL, and PostgreSQL with ExpressJS.


1. MongoDB Integration

MongoDB is a NoSQL database known for its flexibility and scalability. To integrate MongoDB with ExpressJS, you can use the mongoose library, which provides a simple yet powerful way to interact with MongoDB databases.

Example:

const mongoose = require('mongoose');

// Connect to MongoDB database
mongoose.connect('mongodb://localhost/my_database', {
  useNewUrlParser: true,
  useUnifiedTopology: true
}).then(() => {
  console.log('Connected to MongoDB');
}).catch((error) => {
  console.error('Error connecting to MongoDB:', error);
});

This code snippet connects ExpressJS to a MongoDB database named my_database running on the local machine.


2. MySQL Integration

MySQL is a popular relational database management system. To integrate MySQL with ExpressJS, you can use the mysql package, which provides a straightforward interface for interacting with MySQL databases.

Example:

const mysql = require('mysql');

// Create MySQL connection pool
const pool = mysql.createPool({
  connectionLimit: 10,
  host: 'localhost',
  user: 'user',
  password: 'password',
  database: 'my_database'
});

// Execute a SQL query
pool.query('SELECT * FROM my_table', (error, results) => {
  if (error) {
    console.error('Error executing query:', error);
    return;
  }
  console.log('Query results:', results);
});

This code snippet creates a connection pool to a MySQL database named my_database and executes a SELECT query on a table named my_table.


3. PostgreSQL Integration

PostgreSQL is a powerful open-source relational database system. To integrate PostgreSQL with ExpressJS, you can use the pg package, which provides a robust interface for working with PostgreSQL databases.

Example:

const { Pool } = require('pg');

// Create a PostgreSQL connection pool
const pool = new Pool({
  user: 'user',
  host: 'localhost',
  database: 'my_database',
  password: 'password',
  port: 5432,
});

// Execute a SQL query
pool.query('SELECT * FROM my_table', (error, results) => {
  if (error) {
    console.error('Error executing query:', error);
    return;
  }
  console.log('Query results:', results.rows);
});

This code snippet creates a connection pool to a PostgreSQL database named my_database and executes a SELECT query on a table named my_table.


4. Conclusion

Integrating databases like MongoDB, MySQL, and PostgreSQL with ExpressJS empowers developers to build robust and scalable web applications and APIs. By leveraging the appropriate database for your application's needs, you can efficiently manage and store data, ensuring optimal performance and reliability.

Comments