Skip to content

Backend API for an online store built with Node.js, Express, TypeScript, and PostgreSQL. Supports user authentication, product management, and order handling. Developed for the Udacity Full Stack JavaScript Nanodegree.

Notifications You must be signed in to change notification settings

dieaikhlayel/StoreFront-Backend

Repository files navigation

Storefront Backend

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)

Prerequisites

Make sure you have the following installed:

  • Node.js (v14+ recommended)
  • npm
  • PostgreSQL

Database Setup

  1. Check PostgreSQL version:

    postgres --version
  2. Start PostgreSQL:

    sudo su - postgres
  3. Enter PostgreSQL shell:

    psql postgres
  4. Create databases:

    CREATE DATABASE storefront;
    CREATE DATABASE storefront_test;
  5. 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;
  6. Exit PostgreSQL shell:

    \q

Environment Variables

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. ENV determines whether the app uses the dev or test database.


Installing Dependencies

Run the following command in the project root:

npm install

This will install all necessary packages listed in package.json.


Database Migrations

Create the database schema with:

db-migrate up

To reset the test database:

npm run test-down

Running Tests

Execute the test suite:

npm run test-up

This will run unit and integration tests using Jasmine and Supertest.


Starting the Server

Start the API server:

npm run start

By default, the server runs on:

  • Backend API: http://localhost:3000
  • Database: localhost:5432 (PostgreSQL default port)

API Routes

/users

  • 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/:id returns the user along with their last 5 purchases

/products

  • Fields: id, name, price, category

  • Notes:

    • CRUD operations require a valid user token

/orders

  • Fields: id, user_id, status

  • Notes:

    • Tracks orders per user and their status (active or completed)
    • All modifying routes require a token

/order_lists

  • Fields: id, order_id, quantity, product_id

  • Notes:

    • Connects products to orders
    • CRUD operations require authentication

About

Backend API for an online store built with Node.js, Express, TypeScript, and PostgreSQL. Supports user authentication, product management, and order handling. Developed for the Udacity Full Stack JavaScript Nanodegree.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published