REST API JavaScript Application for a storefront backend
-
Run psql Terminal
-
Log into postgres database as user postgres OR log into any database as user postgres
-
Run below commands one by one to set up the test database for the storefront api:
For testing purposes: CREATE DATABASE storefront_db_test; For production purposes: CREATE DATABASE storefront_db; CREATE USER storefront_test_user with encrypted password 'store123'; GRANT ALL ON SCHEMA public to storefront_test_user; GRANT ALL PRIVILEGES ON DATABASE storefront_db_test (or storefront_db for production) to storefront_test_user; ALTER DATABASE storefront_db_test (or storefront_db in production) OWNER TO storefront_test_user;
-
Then run below commands to confirm database and user creation Switch to database storefront_db_test by running:
\c storefront_db_test
Confirm no relations in database:
\dt
-
Open a new terminal and navigate to project root folder i.e Open the project folder in a terminal
-
To install yarn globally npm install yarn -g
-
To install package.json dependencies and other dependencies yarn install
-
In the project root folder, create a new file and name it .env The .env file will store environment variables
-
The following lines are an example of what goes into the .env file. Copy paste into your .env file
POSTGRES_HOST=127.0.0.1 POSTGRES_DB=storefront_db POSTGRES_TEST_DB=storefront_db_test POSTGRES_USER=storefront_test_user POSTGRES_PASSWORD=store123 ENV=dev BCRYPT_PASSWORD=secret-password-to-passageway SALT_ROUNDS=8 TOKEN_SECRET=b1envenu3$
Backend Application: Port 3000 Database: Port 5432
- In your package.json file, change ENV=test to ENV=dev in the "test": script line
- Run "db-migrate up" without quotes to create tables
- Run "yarn watch" (without quotes) to run the server on port 3000 You get the message "Found 0 errors. Watching for file changes." on successful run
- Visit http://localhost:3000/ to confirm from the browser that the app is up and running
Visit below routes as outlined to carry out respective operations:
For test data, navigate to the utils folder and copy paste json lines as needed into Postman.
-
Create user: /users/create [POST]
-
User login: /users/login [POST]
Copy returned token and paste in Postman > Authorization > Bearer Token
-
Return user with id: /users/:userId [GET]
-
Return all users: /users/ [GET]
-
Delete user with id: /users/delete/:id [DELETE]
- Create product: /products/create [POST]
- Return product with id: /products/:prodId [GET]
- Return category products: /products/category/:cat [GET]
- Return all products: /products/ [GET]
- Delete product with id: /products/delete/:id [DELETE]
- Create order: /orders/create [GET]
- Return active orders with user id: /orders/active/:userId [GET]
- Update order status: /orders/update-status [POST]
- Return completed orders with user id: /orders/completed/:userId [GET]
- Return all orders belonging to user: /orders/all/:userId [GET]
- Delete order by id: /orders/delete/:id [DELETE]
To build the project and run tests:
yarn test
yarn prettier
yarn lint