Amay Jain
Amay Jain (@BrajBliss)

Follow

Amay Jain (@BrajBliss)

Follow

How to build a Node.js REST API with PostgreSQL - Part 1

A simple backend system with users, transactions, validation, and documentation using The World's Most Advanced Open Source Relational Database

Amay Jain's photo
Amay Jain
·Nov 18, 2022·

4 min read

How to build a Node.js REST API with PostgreSQL - Part 1

Photo by Lala Azizli on Unsplash

Play this article

Table of contents

  • What is this project about?
  • Prerequisites
  • Getting Started - The Setup and Installation
  • Express Server
  • Recap

What is this project about?

I recently applied to a startup for a backend developer position. I got a response with a backend task to be completed with Node.js, any appropriate database, and documentation using Swagger. I decided to write a tutorial for the same that might help others get started with API development in JavaScript. With that being said, you can check out the complete GitHub Repo.

Prerequisites

I have tried my best to make sure that even beginners can start with this project without needing to know a lot. But there are still a few things you should be familiar with before moving on.

  • Navigating VSCode

  • JavaScript ES6

  • CRUD Operations

  • Relational Database and Queries

You can still start without knowing all of the above, but it will be considerably harder for you to follow through.

Getting Started - The Setup and Installation

IDE and Extensions

We will be using Visual Studio Code for our project. If you do not have it installed, download it from here. Now, there are a lot of settings you might want to change. I will leave that for the VSCode Setup tutorial I will be uploading on YouTube. For now, you can start with whatever setup you have.

npm install dependencies

  1. Create a new folder and open it in VSCode.

  2. Generate an empty project using npm init -y.

  3. Install dependencies npm i express nodemon pg swagger-ui-express.

  4. You can also copy the contents from my package.json file to yours and run npm install which will do the same (preferred method).

  5. Create index.js in your main folder or in src (remember to consider subfolders in the future otherwise you might get file not found errors, I have not used any src folder).

And that is pretty much it for the initial setup. Let's move on to the database. I would recommend reading more about each dependency and why are we using it here:

Database

image.png

Since we are using PostgreSQL, we will be utilizing pgAdmin. Install from here.

pgAdmin is the most popular and feature-rich Open Source administration and development platform for PostgreSQL, the most advanced Open Source database in the world.

  1. Open pgAdmin and set up a server using a password.

  2. As this is out of the scope of this tutorial, you can refer to this YouTube video for more info about setting up a server and creating a table.

  3. Add the following columns to the table with the given properties:

    image.png

  4. This will create unique account IDs, an initial coin balance of 100 for new users, and a name.

  5. Make sure to assign the increment property to the user ID for it to +1 with each new sign-up.

    image.png

And we are good to go. Now it's time to create our server.

Express Server

We will be using the CommonJS Modules instead of ES Modules which might return a warning on the code editor but you can ignore that. If you want, you can perform the same with ES Modules. Edit the package.json file to add the type in order to be able to use ES Modules. Otherwise, just add the start script to use nodemon: "main": "index.js", "type": "module", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "nodemon index.js" },

ES modules are the standard for JavaScript, while CommonJS is the default in Node. js. The ES module format was created to standardize the JavaScript module system. It has become the standard format for encapsulating JavaScript code for reuse.
Read more

  1. Open the index.js file.

  2. // import express from 'express'; -> only if you prefer ES Module const express = require('express'); const app = express(); app.use(express.json()); -> to parse the JSON app.listen(3030, () => console.log('server is up and running')); -> port 3030 module.exports = app;

  3. Considering that you have added the nodemon script in package.json, type npm start in the console in VSCode and it should be working.

image.png

Recap

That is it for this one. Let us quickly revise what we just did and what is coming in the next tutorial.
✅ Learned about dependencies
✅ Creating a PostgreSQL Server using pgAdmin
✅ Initialize an Express App
I know this is a short one but in the upcoming tutorials we will see
🆕 API Endpoints
🆕 User Registration
🆕 Balance Check
🆕 Coin Transfer
🆕 Swagger Documentation
I will be posting more updates on Twitter🐦, you can follow me there. Star the GitHub Repo. Have a good one!

PART 2

Did you find this article valuable?

Support Amay Jain by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this