DEV Community

Cover image for How to Set Up a Node.js Express App with Sequelize and AWS MySQL RDS + Source Code
M Ahsan Hameed
M Ahsan Hameed

Posted on

How to Set Up a Node.js Express App with Sequelize and AWS MySQL RDS + Source Code

Let's Start

Introduction

In this post, we will start our journey of building a Node.js application using Express and Sequelize ORM, while connecting it to an AWS MySQL RDS (Free Tier) database. By the end of this post, we’ll have:

  1. A basic Node.js Express app.
  2. A connection to a MySQL database hosted on AWS RDS.
  3. A working environment with Sequelize ORM to interact with the database.

Let’s dive into it!


Step 1: Setting Up Your Node.js Project

1.1. Install Node.js

Before we begin, make sure you have Node.js installed. If you don’t have it yet, download it from the official website: Node.js Download.

1.2. Initialize a New Node.js Project

Now, let's create a directory for our project and initialize it with npm:

mkdir node-express-mysql
cd node-express-mysql
npm init -y
Enter fullscreen mode Exit fullscreen mode

The -y flag automatically accepts the default settings for your package.json file.

1.3. Install Required Dependencies

Next, let’s install the necessary libraries for our project:

  • express: A minimalist web framework for Node.js.
  • sequelize: An ORM for interacting with the database.
  • mysql2: MySQL client for Sequelize.
  • dotenv: For managing environment variables securely.

Run the following command to install these dependencies:

npm install express sequelize mysql2 dotenv
Enter fullscreen mode Exit fullscreen mode

Step 2: Set Up Your AWS MySQL RDS Database

2.1. Create a MySQL RDS Instance

  • Log in to your AWS Management Console.
  • Go to RDS and click Create database.
  • Choose MySQL as the database engine.
  • For DB Instance Identifier, set a name for your database (e.g., my-mysql-db).
  • Set Master Username and Master Password.
  • Under Settings, choose the Free Tier option for cost-saving.
  • Set up the VPC security group to allow inbound traffic from your IP address (make sure you can access it remotely).
  • Note down the Endpoint, Master Username, and Password once the instance is ready.

2.2. Configure Security Group

After creating your RDS instance, configure the VPC Security Group to allow inbound connections from your local IP. This step ensures that you can access the database from your Node.js application.

Step 3: Set Up Sequelize to Connect to MySQL

3.1. Create a .env File

We will use the dotenv package to securely store our database credentials. Create a .env file in the root of your project:

DB_HOST=your-mysql-db-instance-name.rds.amazonaws.com
DB_USER=admin
DB_PASSWORD=yourpassword
DB_NAME=yourdbname
DB_DIALECT=mysql
Enter fullscreen mode Exit fullscreen mode

Replace the values with your actual database details.

3.2. Sequelize Configuration

Create a file called db.js for configuring the Sequelize connection to your MySQL database.

// db.js
require('dotenv').config();
const { Sequelize } = require('sequelize');

// Set up Sequelize connection
const sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASSWORD, {
  host: process.env.DB_HOST,
  dialect: process.env.DB_DIALECT,
});

module.exports = sequelize;
Enter fullscreen mode Exit fullscreen mode

This file will use the environment variables from the .env file to establish a connection to the MySQL database.

Step 4: Create Your Express Application

4.1. Set Up the Basic Express App

Now let’s create a basic Express app in app.js.

// app.js
const express = require('express');
const sequelize = require('./db');
require('dotenv').config();

const app = express();
const port = 3000;

app.use(express.json());

// Test DB Connection
sequelize.authenticate()
  .then(() => console.log('Database connected successfully!'))
  .catch((error) => console.error('Unable to connect to the database:', error));

app.listen(port, () => {
  console.log(`App running at http://localhost:${port}`);
});
Enter fullscreen mode Exit fullscreen mode

4.2. Test Database Connection

In the above code:

We initialize Express and set it up to listen on port 3000.

We use Sequelize's authenticate method to verify if we can successfully connect to the MySQL database.

If the connection is successful, we’ll see "Database connected successfully!" in the terminal.

4.3. Run the Application

Start the application by running:

node app.js
You should see:
Enter fullscreen mode Exit fullscreen mode
App running at http://localhost:3000
Database connected successfully!
Enter fullscreen mode Exit fullscreen mode

If you see "Database connected successfully!", it means your connection to the MySQL database hosted on AWS RDS is working!

DOWNLOAD SOURCE CODE

Conclusion

In this first post of our learning journey, we’ve:

  • Set up a Node.js Express application.
  • Connected it to a MySQL RDS database using Sequelize ORM.
  • Tested the connection to ensure everything is working smoothly.

In the next post, we’ll move on to creating Sequelize models, define our database tables, and perform basic CRUD operations (Create, Read, Update, Delete).

Next Post Preview

  • We will define Sequelize models (tables) for our app.
  • We’ll dive into CRUD operations using Sequelize.

Stay tuned for the next post, and feel free to drop any questions or feedback below!

Top comments (0)