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.
The API offers several endpoints to access and manipulate data in a Postgres database through both CRUD and custom actions.
PostgreSQL is the object-relational database management system.
A detailed list of the endpoints and actions available can be found in the REQUIREMENTS.md file.
Send Request
button above every endpoint.More About The Project...
RESTful API that supports CRUD for all entities in the database.
Connect to a Postgres database.
Organized Express routes into handlers.
Created Express functions for incoming requests.
Mapped RESTful routes to model methods.
Added endpoint tests.
Available to the public using CORS.
Secure.
Passwords are stored hashed using the bcrypt package.
Allow products to be created and stored in a database.
Allow users to sign up and sign in.
Allow users to login and register.
Require users to be signed in to perform certain actions.
Allow users to create orders and add products to orders.
Added a cart.
Every user can only update the data of himself.
Functionalities - Features
users
products
orders
dashboard
How to setup and connect to the database - Databases Configurations
We are using PostgreSQL as our object-relational database management system to deal with and manipulate databases.
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"}
}
}
npm i
.mig:u
script : To set up all the needed tables and their relationshipsstart
script : To run the serverwhat ports the backend and database are running on
They are running on the ports provided by the user in the .env file.
Usually i run them on the following ports:-
Scripts
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.