Giter Site home page Giter Site logo

simple-api-nodejs-express-postgresql's Introduction

Simple Products API - Node.js (Express), PostgreSQL

This app consists of a simple API logic written using Node.js (ESM), Express, Knex, PostgreSQL. No authorization was used in the project. Simple seeds and migrations used in the project are available in the folder.


Installing

  • To install just do this :-)
npm install
  • All dependencies of the app are in the package.json file
  • Dependencies to install:
"dependencies": {
    "express": "^4.19.2",
    "knex": "^3.1.0",
    "pg": "^8.11.5"
  }

Database (PostgreSQL)

You will need to download PostgreSQL and configure it. All migration files and seeds files are in the project.

data/migrations
data/seeds

When you want to set up the database connection, unless you are working in production, it will be sufficient to make changes in the development section of the knexfile.js file in the main directory.

connection: {
            database: "app",
            user: "admin",
            password: "admin"
        }
  • Command line to migrate migrations:
knex migrate:up
knex migrate:latest
  • Command line to add seeds:
knex seed:run

IMPORTANT!!!

Note: If you get an "undefined command" error when running the knex command, you will need to install knex globally.

npm install -g knex

Note: If tables are not created in the database after migration, delete the knex_migrations and knex_migrations_lock tables created in the database and try to migrate again.


Helpful command lines (PostgreSQL)

Here are some helpful commands for you to access and use on 'PowerShell' after installing the PostgreSQL database.

Suggestion: Using this site or other database design tools and visualizing your thoughts while adding tables to the database will always make you feel comfortable. Link: https://app.dbdesigner.net/

To connect to the database:

psql -d postgres -U <password>

Get user list

\du;

Database list

\list;

Table list

\dt;

Get table

SELECT * FROM <table_name>;

To connect to an internal database and switch to the command line:

\c <database_name>;

To export the database as .sql:

pg_dump -U <username> -d <database> -f <db_name.sql>

To role the user to create a database:

ALTER ROLE <username> CREATEROLE CREATEDB;

API Structure

There are two extra simple 'middleware' within the project. logger.js to log the requests to the console and errorHandling.js to obtain the status code and error description of the errors.

Executing program

When you run the program without changing anything, it will run on port 5000 by default. Example for localhost:

http://localhost:5000

GET method queries

Getting the list of all products in JSON format.

http://localhost:5000/products/getall

Requesting a product by ID

http://localhost:5000/products/get/1

POST method queries (You can try with postman)

Add new product

http://localhost:5000/products/create

Example data to be sent:

{
    "title": "AirPods Pro",
    "descriptions": "AirPods Pro are dust resistant....",
    "price": 850.45,
    "category_id": 3,
    "brand_id": 1
}

DELETE method queries

To delete the product

http://localhost:5000/products/delete/3

PATCH method queries

  http://localhost:5000/products/update/31

Example data to be sent:

{
    "title": "AirPods Pro V6",
    "descriptions": "AirPods Pro are dust resistant....",
    "price": 830.45,
    "category_id": 3,
    "brand_id": 1
}

Some screenshots







Authors

Kamran Karimov

simple-api-nodejs-express-postgresql's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.