This project is part of the Udacity Full Stack JavaScript Nanodegree.
The goal is to create a backend API for a storefront application based on the requirements outlined in REQUIREMENTS.md.
The API demonstrates:
- PostgreSQL database integration in Node.js
- Database migrations using
db-migrate - Structuring routes, models, and HTTP verbs
- Test-driven development with Jasmine and Supertest
- Password hashing using bcrypt
- Securing routes with JWT (JSON Web Tokens)
Make sure you have the following installed:
- Node.js (v14+ recommended)
- npm
- PostgreSQL
-
Check PostgreSQL version:
postgres --version
-
Start PostgreSQL:
sudo su - postgres
-
Enter PostgreSQL shell:
psql postgres
-
Create databases:
CREATE DATABASE storefront; CREATE DATABASE storefront_test;
-
Create a database user and grant privileges:
CREATE USER storefront_user WITH PASSWORD 'your_password'; GRANT ALL PRIVILEGES ON DATABASE storefront TO storefront_user; GRANT ALL PRIVILEGES ON DATABASE storefront_test TO storefront_user;
-
Exit PostgreSQL shell:
\q
Create a .env file in the root directory with the following:
DB_HOST=postgres
DB_NAME=storefront
DB_USER=storefront
DB_PASSWORD=store123
TEST_DB_NAME=storedb_test
ENV=dev
BCRYPT_PW=your_pepper_string
SALT_ROUNDS=10
TOKEN_SECRET=your_jwt_secret
PROJECT_PATH=.Adjust these values to match your setup.
ENVdetermines whether the app uses the dev or test database.
Run the following command in the project root:
npm installThis will install all necessary packages listed in package.json.
Create the database schema with:
db-migrate upTo reset the test database:
npm run test-downExecute the test suite:
npm run test-upThis will run unit and integration tests using Jasmine and Supertest.
Start the API server:
npm run startBy default, the server runs on:
- Backend API:
http://localhost:3000 - Database:
localhost:5432(PostgreSQL default port)
-
Fields:
id,first_name,last_name,password,recentPurchases(optional) -
Notes:
- Creating a user does not require authentication
- Login is available at
/users/login - Passwords are hashed with bcrypt
- GET
/users/:idreturns the user along with their last 5 purchases
-
Fields:
id,name,price,category -
Notes:
- CRUD operations require a valid user token
-
Fields:
id,user_id,status -
Notes:
- Tracks orders per user and their status (
activeorcompleted) - All modifying routes require a token
- Tracks orders per user and their status (
-
Fields:
id,order_id,quantity,product_id -
Notes:
- Connects products to orders
- CRUD operations require authentication