DEV Community

Cover image for Express - Node.js API with PostgreSQL
Jawher Kallel
Jawher Kallel

Posted on

Express - Node.js API with PostgreSQL

Building a robust and efficient RESTful API is a fundamental skill for modern web development. In this article, we'll explore how to create a Node.js API integrated with PostgreSQL, leveraging the repository node-api-postgres as a practical example.

Introduction

The node-api-postgres repository provides a well-structured template for setting up a RESTful API using Node.js and PostgreSQL. This setup is ideal for applications requiring a reliable backend with database interactions.

Features

  • User Management: Create, retrieve, update, and delete user records.
  • Authentication: Secure endpoints using JSON Web Tokens (JWT).
  • Data Validation: Ensure data integrity with validation mechanisms.

Technologies Used

  • Node.js: A JavaScript runtime for building scalable network applications.
  • Express.js: A minimal and flexible Node.js web application framework.
  • PostgreSQL: A powerful, open-source relational database system.
  • Mocha: A feature-rich JavaScript test framework running on Node.js and in the browser.
  • Swagger: A collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
  • JWT: A compact, URL-safe means of representing claims for secure communication.

Getting Started

Prerequisites

Ensure you have the following installed:

  • Node.js
  • PostgreSQL
  • npm (Node Package Manager)

Installation Steps

  1. Clone the Repository:
   git clone https://github.com/JawherKl/node-api-postgres.git
   cd node-api-postgres
Enter fullscreen mode Exit fullscreen mode
  1. Install Dependencies:
   npm install
Enter fullscreen mode Exit fullscreen mode
  1. Configure the Database:
  • Create a PostgreSQL database named node_api.
  1. Start the Server:
   npm start
Enter fullscreen mode Exit fullscreen mode

The API will be accessible at http://localhost:3000.

  1. Run Unit Test:
   npm test
Enter fullscreen mode Exit fullscreen mode

API Endpoints

The API provides the following endpoints for user management:

  • GET /users: Retrieve all users.
  • GET /users/:id: Retrieve a user by ID.
  • POST /users: Create a new user.
  • PUT /users/:id: Update an existing user.
  • DELETE /users/:id: Delete a user.

Authentication is handled via JWT. Protected routes require a valid token in the Authorization header.

Project Structure

The repository follows a modular structure:

node-api-postgres/
├── config/
├── controllers/
├── middleware/
├── models/
├── routes/
├── test/
└── utils/
Enter fullscreen mode Exit fullscreen mode
  • config/: Database configuration files.
  • controllers/: Functions handling API requests.
  • middleware/: For process incoming requests.
  • models/: Many models representing database tables.
  • routes/: API route definitions.
  • test/: Unit test for all users api.
  • utils/: Utility functions and helpers.

Conclusion

The node-api-postgres repository serves as an excellent starting point for building RESTful APIs with Node.js and PostgreSQL. Its modular design and inclusion of essential features like authentication and data validation make it a valuable resource for developers.

For a visual walkthrough of building a similar API, you might find the following video helpful:

https://www.youtube.com/watch?v=VIUEblsGwL4

Top comments (0)