REST API with Express.js
PostgreSQL Example

This Example will show you how to make a REST API using Express.js and PostgreSQL.

The database of the example contains two tables: book and user. The user will be used to authorize the REST API users. So, in the exercise you will build a login system, so that the credentials will be checked based on the user.

The book -table has these fields:

  • id_book INT primary key
  • name VARCHAR(255)
  • author VARCHAR(255)
  • isbn VARCHAR(20)
And the user -table has these fields:
  • id_user INT primary key
  • username VARCHAR(20) UNIQUE
  • password VARCHAR(255)

There is no big differences, between using MySQL or PostgreSQL database. So, I have added here only the code for Book Controller and Model. and Here are the most notable diffrences:

MySQL PostgreSQL
response.json(dbResult); response.json(dbResult.rows);
insert into book_table (name, author, isbn) values(?,?,?) insert into book_table (name, author, isbn) values($1,$2,$3);



Toggle Menu