A Comprehensive Guide to Setting Up an Express and Sequelize Project from Start to Finish

Shir Hussain Tabesh
4 min readDec 3, 2023

Based on my experience, I’ll provide a comprehensive and detailed guide to setting up an Express js project that uses Sequelize ORM to interact with database. Howver, before going through this guide, you need to be familiar with Express JS and Sequelize. Express JS is a framework for Node JS. Similarly, Sequelize is a library for Node JS. Node JS is a runtime environment for executing JavaScript codes.

Content:

  • Structure of the project (Directory Tree)
  • .gitignore
  • git and github
  • .env
  • Sequelize setup
  • Express setup

Structure of the project (Directory Tree)

In order to have an organized project and feel comfortable moving back and forth in your project files, you need to structure it well. That means separate all the files based on their relevance: backend, frontend, .gitignore, and README.md

.gitignore

When it comes to Software Development, most probabaly you use git and github to do a version control and collaboratively work on the same project with your friends or other developers. However, you need to keep some files on your computer. in this file, you can manage which files not to commit to github. It includes some critical file not to share for security reasons or some unnecessary resources that you don’t need to upload them on github. You write the name of all files in the .gitignore file.

However, the .gitignore file itself is commited to github. So others know which files are ignored to commit. Don’t forget that the .gitignore file is in the root of your Git repository. Here are some examples:

  • node_modules/
  • .env
  • *.exe
  • *.db

Git

Git is used to keep the track of code changes. That is why it is called a version control system. You may create multipel branches and work on them. At the end, you can merge them. However, to set the main branch as the default branch and start working on your project, run this code on your terminal from the root of your project:

git config --global init.defaultBranch main
git init

GitHub repository

GitHub is a web-based platform that uses Git for version control, facilitating collaborative software development and code sharing.

Create your repository on github, then add its URL to remote origin of your project. Go to your terminal at the root of the project, and execute this git command:

git remote add origin <github-repository- url>
git add .
git commit -m 'initial commit'
git push origin main

.env

.env is a configuration file uses to store environment variables. It generally contains the sensitive information such as API keys, database connection and other configuration settings in a key-value pairs. That is why it is added in the .gitignore file. By keeping this information in a separate file, developers can maintain a clear separation between code and configuration, and it allows for easy customization of settings based on the deployment environment like development, testing, and production. It looks like this:

PORT = 5001
DB_FILE = db/dev.db
JWT_SECRET = key

In your coding, your can use process.env.<environment variable>

const port = process.env.PORT || 5001
const secret = process.env.JWT_SECRET

You can use dotenv to access .env variables in the development environment. For instance, when seeding the demo data into the database in development environment, use dotenv with sequelize command to access the database:

npx dotenv sequelize db:seed:all

Sequelize Setup (.sequelizerc)

.sequelizerc is a configuration file that is used to customize the project structure. The main elements of a Sequelize project are models, migrations, and seeders. Therefore, before initiating your Sequelize project, create the .sequelierc. It looks like this:

const path = require('path');

module.exports = {
config: path.resolve('config', 'database.js'),
'models-path': path.resolve('db', 'models'),
'seeders-path': path.resolve('db', 'seeders'),
'migrations-path': path.resolve('db', 'migrations')
};

Now you can initiate your Sequelize project:

npm install sequelize
npx sequelize init

Then you can create the models, migrations or seeders via terminal. They are automatically located in their relevant directories.

npx sequelize model:generate --name ModelName --attributes col1:datatype,...

In config directory, open the database.js, set your database and environments. A sample would look like this:

module.exports = {
development: {
storage: process.env.DB_FILE,
dialect: "sqlite",
seederStorage: "sequelize",
logQueryParameters: true,
typeValidation: true
},
production: {
use_env_variable: 'DATABASE_URL',
dialect: 'postgres',
seederStorage: 'sequelize',
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false
}
},
define: {
schema: process.env.SCHEMA
}
}
};

Express Setup

After completeing the Sequelize setup, you can start working on your Express application setup.

to initialize your Express application, create a file named app.js in your backend directory. Then import the ‘express’ file at the top as bellow:

const express = require('express')
const app = express()
// middlewares and routes are defined here...

const PORT = process.env.PORT || 5001;
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});

module.exports = app;

This note is being in development. If you have any specific questions, you can add your questions as comments.

--

--