DEV Community

Cover image for React + Node.js + PostgreSQL: CRUD example
Tien Nguyen
Tien Nguyen

Posted on • Updated on

React + Node.js + PostgreSQL: CRUD example

In this tutorial, I will show you how to build full-stack React + Node.js + PostgreSQL CRUD example. The back-end server uses Node.js + Express for REST APIs, front-end side is a React.js client with React Router, Axios & Bootstrap.

Full Article: https://bezkoder.com/react-node-express-postgresql/

React + Node.js + PostgreSQL CRUD example

We will build a full-stack Tutorial Application in that:

  • Tutorial has id, title, description, published status.
  • User can create, retrieve, update, delete Tutorials.
  • There is a search box for finding Tutorials by title.

Here are screenshots of the example.

  • Add a Tutorial:

react-node-js-postgresql-crud-example-create

– Show all objects:

react-node-js-postgresql-crud-example-retrieve-all

– Click on Edit button to access an object:

react-node-js-postgresql-crud-example-retrieve-one

On this Page, you can:

  • change status to Published/Pending using Publish/UnPublished button
  • remove the object from PostgreSQL Database using Delete button
  • update this object's details on Database with Update button

react-node-js-postgresql-crud-example-update

  • Search objects by field 'title':

react-node-js-postgresql-crud-example-search

  • Check PostgreSQL database:
testdb=# select * from tutorials;
 id |               title                |          description          | published |         createdAt          |         updatedAt
----+------------------------------------+-------------------------------+-----------+----------------------------+----------------------------
  5 | PostgreSQL Tut#1                   | Tut#1 Description             | f         | 2020-03-11 09:14:01.773+07 | 2020-03-11 09:14:01.773+07
  6 | React.js Tut#2                     | Tut#2 Description             | f         | 2020-03-11 09:15:05.629+07 | 2020-03-11 09:15:05.629+07
  8 | React Hooks Tut#4                  | Tut#4 Description             | f         | 2020-03-11 09:24:33.295+07 | 2020-03-11 09:24:33.295+07
  9 | React Express PostgreSQL Tut#5     | Tut#5 Description             | f         | 2020-03-11 09:24:53.463+07 | 2020-03-11 09:24:53.463+07
  7 | Node. Express PostgreSQL (updated) | This is Description for Tut#3 | t         | 2020-03-11 09:22:53.635+07 | 2020-03-11 09:28:45.568+07
(5 rows)

React, Node.js, PostgreSQL CRUD Architecture

This is architecture of our system:

react-node-js-postgresql-crud-example-architecture

  • Node.js Express exports REST APIs & interacts with PostgreSQL Database using Sequelize ORM.
  • React Client sends HTTP Requests and retrieves HTTP Responses using Axios, consume data on the components. React Router is used for navigating to pages.

Video

This is our React Node.js PostgreSQL CRUD application with Express & Sequelize demo:

Node.js Express Back-end

These are APIs that Node.js Express App will export:

Methods Urls Actions
GET api/tutorials get all Tutorials
GET api/tutorials/:id get Tutorial by id
POST api/tutorials add new Tutorial
PUT api/tutorials/:id update Tutorial by id
DELETE api/tutorials/:id remove Tutorial by id
DELETE api/tutorials remove all Tutorials
GET api/tutorials?title=[kw] find all Tutorials which title contains 'kw'

React.js Front-end

react-node-js-postgresql-crud-example-client-components

– The App component is a container with React Router. It has navbar that links to routes paths.

TutorialsList component gets and displays Tutorials.
Tutorial component has form for editing Tutorial's details based on :id.
AddTutorial component has form for submission new Tutorial.

– These Components call TutorialDataService methods which use axios to make HTTP requests and receive responses.

For more details, implementation and Github, please visit:
https://bezkoder.com/react-node-express-postgresql/

Further Reading

Run both projects in one place:
How to Integrate React with Node.js Express on same Server/Port

With Pagination:
React Pagination with API using Material-UI

react-node-js-postgresql-crud-example-pagination-client

Or Serverless with Firebase:

Happy learning, see you again!

Top comments (0)