Skip to content

lilithrv/acceso-db-node-postgres-pg-ii

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Desafio Latam: Database Access with Node II

The social network "Like Me" is developing its platform that allows the creation and deletion of posts and the interaction of likes.

In this challenge, a server is created that provides GET, POST, PUT and DELETE routes for queries and saves the posts in a PostgreSQL database with the pg package.

The server provides the following routes:

  • GET: api/posts : Returns a JSON with the registered posts from the database
  • GET: api/posts/:id : Returns a specific post from the database
  • POST: api/posts : Allows you to enter a new post
  • PUT: api/posts/like/:id : Allows you to increase likes to a post
  • DELETE: api/posts/:id : Receives the id of a post and removes it from the database

Environment variables

Connects node.js to the PostgreSQL server. To specify which database to connect to, create an .env file with the following structure, also available in the .env.example file.

To create the database follow the instruction of the query.sql file.

.env

PGUSER=postgres 
PGHOST=localhost
PGPASSWORD=
PGDATABASE=likeme
PGPORT=5432

Client Application

The client application that consumes the routes is located in the client folder and its a React application. To use it, inside the client directory, run in the terminal npm run dev after the backend is up:

  • POST: Fill in the 3 fields and click on add, you will see the post created on the right side of the screen.
  • PUT: To like a post, click on the heart icon.
  • DELETE: To delete a post, click on the cross.

Using Thunder Client for VS Code or Postman as a client application


To get all posts:

METHOD: GET 
ENDPOINT: localhost:3000/api/posts/

To get a specific post:

METHOD: GET
ENDPOINT: localhost:3000/api/posts/{id}

To add a post, the following structure must be followed (the id is serial so, to create a post you don't need to write this data):

METHOD: POST
ENDPOINT: localhost:3000/api/posts/
BODY JSON

{
    "titulo": "",
    "img": "",
    "descripcion": ""
}

To like a post:

METHOD: PUT
ENDPOINT: localhost:3000/api/posts/like/{id}

To delete a post:

METHOD: DELETE
ENDPOINT: localhost:3000/api/posts/{id}

Database

Backend

Dependencies

CLIENT:

  • This react project was created with Vite
  • Axios : A promise-based HTTP Client for node.js and the browser
  • The React-toastify library was also used to implement notifications on the app
  • To install dependencies run: npm install inside the client folder
  • To run de app: npm run dev

SERVER:

  • Framework Express
  • CORS : For providing a Connect/Express middleware that can be used to enable CORS with various options
  • node-postgres: pg : Collection of node.js modules to interact with PostgreSQL database
  • Environment variables dotenv
  • To install dependencies run: npm install inside the server folder
  • devDependencies Nodemon for run server and automatically restarting the node application when file changes, in the terminal run: npm run dev

Important: Run first the backend and then run the client application

About

Desafio Latam: Database Access with Node II

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published