Featured Article

Build a MySQL Node js Express Example Fast With APIs

Adam Jul 13, 2026

In the dynamic world of web development, the stack that includes MySQL, Node.js, and Express.js has become a powerful trifecta. This combination enables developers to create robust, scalable, and efficient web applications. Let's explore a practical example of integrating MySQL, Node.js, and Express.js to understand how they work together.

NodeJS Import CSV To Database
NodeJS Import CSV To Database

Before diving into the example, let's briefly understand each component:

Node.js Express Login example with MySQL database
Node.js Express Login example with MySQL database

MySQL

MySQL is an open-source relational database management system. It's known for its performance, reliability, and ease of use, making it a popular choice for web applications.

Express.js Cheat Sheet for Web Development | Backend & Routing Notes | AKTU Notes
Express.js Cheat Sheet for Web Development | Backend & Routing Notes | AKTU Notes

Node.js and Express.js will interface with MySQL using a library called 'mysql2', which is a maintained fork of the 'mysql' library.

Node.js and Express.js

an image of a computer screen with some text on it
an image of a computer screen with some text on it

Node.js is a JavaScript runtime that allows you to run JavaScript on a server. It's efficient, non-blocking, and perfect for creating fast, data-intensive real-time applications.

Express.js, on the other hand, is a minimalistic web application framework for Node.js. It provides a robust set of features for building single and multi-page, and hybrid web applications.

Setting up the Project and MySQL Connection

HTTP method express
HTTP method express

First, initialize a new Node.js project using npm (Node Package Manager) if you haven't already:

npm init -y

Then install 'express', 'mysql2', and 'dotenv' (to manage environment variables) using npm:

npm install express mysql2 dotenv

In your '.env' file, store your MySQL credentials:

Understanding routing of expressJS in Node.JS
Understanding routing of expressJS in Node.JS

DB_HOST=your_host
DB_USER=your_user
DB_PASSWORD=your_password
DB_NAME=your_database

Creating the MySQL Connection

Connect to MySQL using the credentials from the '.env' file:

Express.js Cheat Sheet | Backend Development & REST API Guide πŸš€
Express.js Cheat Sheet | Backend Development & REST API Guide πŸš€
Simplify Input Validation with Zod in Node.js
Simplify Input Validation with Zod in Node.js
Read CSV Files In NodeJS
Read CSV Files In NodeJS
Express js vs Node js
Express js vs Node js
Node.js Cheat Sheet for Web Development | Backend Basics & NPM Guide | AKTU Notes
Node.js Cheat Sheet for Web Development | Backend Basics & NPM Guide | AKTU Notes
Node.js roadmap
Node.js roadmap
π—‘π—Όπ—±π—²π—·π˜€ console module provides a π—–π—Όπ—»π˜€π—Όπ—Ήπ—² class
π—‘π—Όπ—±π—²π—·π˜€ console module provides a π—–π—Όπ—»π˜€π—Όπ—Ήπ—² class
Building a Simple Authentication System with Express and JWT
Building a Simple Authentication System with Express and JWT
Express.js Cheat Sheet for Beginners β€” Essential Syntax and Commands
Express.js Cheat Sheet for Beginners β€” Essential Syntax and Commands
Node.JS Cheatsheet
Node.JS Cheatsheet
an image of a computer screen with some type of text on it and another line of code in the background
an image of a computer screen with some type of text on it and another line of code in the background
Top NodeJS Framework
Top NodeJS Framework
Crud Operation in Nodejs and MySQL with Source Code
Crud Operation in Nodejs and MySQL with Source Code
JavaScript vs Node JS
JavaScript vs Node JS
Express.js Cheat Sheet
Express.js Cheat Sheet
an image of a computer screen with some text on the bottom right corner and another line of code above it
an image of a computer screen with some text on the bottom right corner and another line of code above it
Best Node.js Project Ideas
Best Node.js Project Ideas
Node.js Developer Roadmap 2026: Master Backend Engineering (Step-by-Step) πŸš€
Node.js Developer Roadmap 2026: Master Backend Engineering (Step-by-Step) πŸš€
Node js
Node js
Express.js Cheat Sheet for Beginners | Node.js API & Backend Quick Guide
Express.js Cheat Sheet for Beginners | Node.js API & Backend Quick Guide

```javascript const dotenv = require('dotenv'); const mysql = require('mysql2/promise'); dotenv.config(); (async () => { try { const connection = await mysql.createConnection({ host: process.env.DB_HOST, user: process.env.DB_USER, password: process.env.DB_PASSWORD, database: process.env.DB_NAME }); console.log('Connected to the database'); } catch (err) { console.error('Error connecting to the database', err); } })(); ```

Now that we have a connection, we can interact with our MySQL database using Node.js and Express.js.

Interacting with MySQL using Express.js

Create an Express.js server to execute SQL queries and handle responses from the client:

Fetching Data from MySQL

Let's create an endpoint to fetch data from MySQL:

```javascript const express = require('express'); const router = express.Router(); router.get('/api/data', async (req, res) => { const [rows, fields] = await connection.execute('SELECT * FROM your_table'); res.json(rows); }); module.exports = router; ```

In this example, Express.js handles the GET request to '/api/data' and returns data from the MySQL table.

Inserting Data into MySQL

Similarly, you can create an endpoint to insert data into MySQL:

```javascript router.post('/api/data', async (req, res) => { const result = await connection.execute( 'INSERT INTO your_table (column1, column2) VALUES (?, ?)', [req.body.column1, req.body.column2] ); res.json({ message: 'Data inserted successfully', insertId: result[0].insertId }); }); ```

Express.js handles the POST request to '/api/data' and inserts data into the MySQL table.

This is just the beginning. With MySQL, Node.js, and Express.js, you can build complex web applications with efficient data handling.

Embracing this powerful stack can boost your productivity and create applications that can scale with your business. Whether you're a seasoned developer or just starting, diving into MySQL, Node.js, and Express.js is an exciting journey well worth taking.