khalidmesbah / storefront-backend-api

A RESTful JSON Node API backed by a PostgreSQL database for a shopping website.
Other
0 stars 0 forks source link
api authentication authorization backend bycrypt crud express expressjs hashing http jasmine javascript jwt jwt-authentication nodejs postgresql sql testing typescript unit-testing

Storefront Backend API

Advanced Full-Stack Web Development Nanodegree Program - Building an API with Postgres and Express!

About The Project

  • A RESTful JSON Node API backed by PostgreSQL database for a shopping website.

Open the psql command-line tool:-

sudo su - postgres --> Switch to the postgres user 
psql postgres --> Start psql 

We need to make two databases:-

Run the following Queries:-

CREATE USER user_name WITH PASSWORD 'user_password'; 
CREATE DATABASE store_dev;
\c store_dev
GRANT ALL PRIVILEGES ON DATABASE store_dev TO user_name;
CREATE DATABASE store_test;
\c store_test
GRANT ALL PRIVILEGES ON DATABASE store_test TO user_name;

Create a .env file according to the .env.example file to set up the environment variables.

Create a database.json file like the following :-

{
    "defaultEnv": {"ENV": "NODE_ENV"},
    "dev": {
        "driver": "pg",
        "host": {"ENV": "POSTGRES_HOST"},
        "port": {"ENV": "POSTGRES_PORT"},
        "database": {"ENV": "POSTGRES_DB"},
        "user": {"ENV": "POSTGRES_USER"},
        "password": {"ENV": "POSTGRES_PASSWORD"}
    },
    "test": {
        "driver": "pg",
        "host": {"ENV": "POSTGRES_HOST"},
        "port": {"ENV": "POSTGRES_PORT"},
        "database": {"ENV": "POSTGRES_DB_TEST"},
        "user": {"ENV": "POSTGRES_USER"},
        "password": {"ENV": "POSTGRES_PASSWORD"}
    }
}

Usually i run them on the following ports:-

    npm i # To install all the dependencies needed for the project.
    npm run mig:u # To Call the up migrations.
    npm run mig:d # To call the down migrations.
    npm run mig:r # To reset all migrations.
    npm run build # To compile typescript.
    npm run start # To run the server.
    npm run dev # To run the server for development.
    npm run watch # To run the watcher.
    npm run format # To format the code.
    npm run lint # To accelerate development and reduce errors.
    npm run lint:fix # To fix developers errors.
    npm run test # To test the Project.

Technologies / Stack

  • PostgreSQL.
  • Node.js.
  • Express.
  • TypeScript.
  • Jasmine.
  • JavaScript.

    Middlewares And Utilities

  • cors
  • express.json
  • morgan
  • helmet
  • error.middleware
  • authenticateToken
  • rateLimit
  • verifyAuthToken
  • parseJWT
  • genereateRandom
  • hashPassword

    Dependencies And Dev-Dependencies

  • Prettier : for code formating.
  • Eslint : for improving style and structure.
  • dotenv : for managing environment variables.
  • db-migrate : for migrations.
  • pg : for connecting to a database.
  • jsonwebtoken : for working with JWTs.
  • jasmine : for test driven development.
  • morgan : for logging incoming requests to the server.
  • helment : for securing the server.
  • express : for CORS and route handling.
  • express.json : for parsing incoming requests.
  • express-rate-limit : for limiting the number of requests to the server.
  • bcrypt : for encrypting the passwords .
  • jwt : for authorization.
  • TypeScript : for reducing type errors.
  • Supertest : for testing the api
  • node : for coding the logic.
  • nodemon : for auto running the server.