How to Get Started with Express, Sequelize and Passport
By: Wajahat Zia
Tags: Node Js Tutorial, Sequelize, Passportjs, Expressjs, JavaScript

As a person who is just getting started with Node.js and API building, you will definitely run into major errors and confusions. To save yourself some time and spending countless hours on Google Search, I’ll demonstrate how to set up a simple Express API with Node, Sequelize and Passport.js integration. On a side note, we will be using PostgreSQL as a database with Sequelize and hopefully you find this article helping with your journey as a Web Developer.

Getting Started

Before we begin, I’ll assume you have Node.js and PostgreSQL setup on your system. There are numerous articles out there that help with this so I’ll skip this part and jump right into it

Step 1: Setting up your database

In my scenario, I’m using PgAdmin 4 to play around with my Postgres database. Open PgAdmin and start with creating a new database. Right click on “Databases” listed in the left hand side pane and select “Create Database”

Give your database a simple name and click ‘save’. This should refresh the left pane and your new database will appear there. That is pretty much it with setting up your Database.

Step 2: Setting up Node

Begin by creating an empty folder to house your project files. You can name your project folder anything you like. I like to use Visual Studio Code to keep my code and files organized. If you are using another editor, feel free to follow along as most of the commands will be same.

Open up a terminal inside the directory of the project folder you created. As I named my folder “Express-Sequelize-Passport”, my terminal shows this:

C:\Users\author\Desktop\Express-Sequel-Passport>

Start by typing “npm init” and perform the steps as shown below:

package name: (express-sequel-passport)

version: (1.0.0)

description:

entry point: (index.js) app.js

test command:

git repository:

keywords:

author:

license: (ISC)

You can keep pressing enter to have these fields pop up one by one. The only change I did was to change my “entry point” to “app.js”. This keeps my directory structure tidy and helps me remember where what code is. On a side note, the “entry point” is where your main code will run.

Once you have completed these steps, you’ll notice a “package.json” file created in the directory. That is good. You are now ready to move on to the next step.

Step 3: Directory Structure

A big part of writing clean code is to have a clean directory structure. With that said, I have attached a directory tree that you should replicate as shown by creating the necessary folders. Typing ‘tree’ in the terminal will show you your directory tree

└───Express-Sequel-Passport

├───config

└───src

├───db

├───models

└───routes

Step 4: Install node modules

In your project folder, open up the terminal and copy paste the following code to install the necessary modules that we’ll use along the way

npm install --save express nodemon passport passport-local dotenv body-parser express-session morgan pg pg-hstore sequelize

I’ll explain each of these modules as we build our code. So don’t worry as to what each modules does for now.

Step 5: Add Nodemon and Babel

Nodemon helps with automatically restarting your script when ever you make changes to your code. This is a very convenient tool to have. As we have installed nodemon in the previous steps, lets configure our package.json file to use it. Make the changes as shown below:

Be sure to add the correct path to your ‘app.js’.