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
Photo by Lala Azizli on Unsplash
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
Create a new folder and open it in VSCode.
Generate an empty project using
npm init -y
.Install dependencies
npm i express nodemon pg swagger-ui-express
.You can also copy the contents from my package.json file to yours and run
npm install
which will do the same (preferred method).Create
index.js
in your main folder or insrc
(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
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.
Open pgAdmin and set up a server using a password.
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.
Add the following columns to the table with the given properties:
This will create unique account IDs, an initial coin balance of 100 for new users, and a name.
Make sure to assign the increment property to the user ID for it to +1 with each new sign-up.
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
Open the
index.js
file.// 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;
Considering that you have added the nodemon script in
package.json
, typenpm start
in the console in VSCode and it should be working.
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!
Did you find this article valuable?
Support Amay Jain by becoming a sponsor. Any amount is appreciated!