Skip to content

samanr/storefront-api

Repository files navigation

Storefront Backend API

A RESTful API for an online storefront built with Node.js, Express, TypeScript, and PostgreSQL.

Prerequisites

Ports

Service Port
API Server 3000
PostgreSQL 5432

Environment Setup

Create a .env file in the project root with the following variables:

POSTGRES_DB=full_stack_dev
POSTGRES_USER=full_stack_user
POSTGRES_PASSWORD=password123
POSTGRES_HOST=127.0.0.1
POSTGRES_PORT=5432
BCRYPT_PASSWORD=your-bcrypt-secret
SALT_ROUNDS=10
TOKEN_SECRET=your-jwt-secret

Package Installation

npm install

Database Setup

1. Start the PostgreSQL container

Ensure Docker Desktop is running, then:

docker compose up -d

This starts a PostgreSQL 16 container on port 5432 using the credentials from your .env file.

2. Verify the database connection

docker compose exec postgres psql -U full_stack_user -d full_stack_dev

Once inside psql, run \dt to list tables. Type \q to exit.

3. Run database migrations

npx db-migrate up

This creates all required tables: users, products, orders, order_products.

4. Seed an admin user

Generate a bcrypt hash for your chosen password (replace values to match your .env):

node -e "const b=require('bcrypt'); console.log(b.hashSync('your-password' + 'your-bcrypt-secret', 10))"

Then insert the admin user via psql:

docker compose exec postgres psql -U full_stack_user -d full_stack_dev
INSERT INTO users (first_name, last_name, password) VALUES ('Admin', 'User', '<paste_hash_here>');

Running the Server

npm start

The API will be available at http://localhost:3000.

Available Scripts

Script Description
npm start Start the server with ts-node
npm run watch Start with auto-reload on changes
npm run lint Run ESLint on TypeScript files
npm run prettier Format TypeScript files
npm test Run Jasmine tests

Authentication

All protected routes require a JWT token in the Authorization header:

Authorization: Bearer <token>

Getting a token

Send a POST request to /api/login:

POST http://localhost:3000/api/login
Content-Type: application/json

{
  "first_name": "Admin",
  "last_name": "User",
  "password": "your-password"
}

Use the returned token value in the Authorization header for all protected requests.

API Routes

See REQUIREMENTS.md for full API documentation including request/response shapes.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors