A Node.js + Express backend that powers the ClothingShop mobile application. It exposes REST endpoints for browsing products, managing user accounts, and keeping track of cart contents in a PostgreSQL database.
- Product catalogue with optional gender-based filtering and keyword search.
- Email/password authentication with hashed credentials and JWT-based sessions.
- Persistent shopping cart per user with quantity management.
- Category listing for quick navigation in the client application.
- Node.js with Express
- PostgreSQL via the official
pgdriver - bcryptjs for password hashing
- jsonwebtoken for issuing JWT access tokens
- Node.js 18+
- npm 9+
- Docker (optional, for running PostgreSQL locally via Docker Compose)
-
Install dependencies
npm install
-
Configure environment variables
cp .env.example .env
Update
.envwith the connection details for your PostgreSQL instance and choose a strongJWT_SECRETfor token signing. -
Start PostgreSQL
-
Quick start (recommended):
docker compose up -d
This launches a local PostgreSQL 16 container with the credentials that match the defaults found in
.env.example(myuser/mypassword, databasemydatabase). -
Alternatively, point the app at any PostgreSQL instance and make sure the credentials in your
.envfile match the remote server.
-
-
Create the schema and seed data
node setup-database.js
The script drops and recreates the tables (
users,categories,products,carts,cart_items) and inserts a small set of demo products and categories for local testing. -
Start the development server
node server.js
By default the API listens on
http://localhost:3000(configurable via thePORTenvironment variable).
Configuration is now driven via environment variables (loaded from .env during local development). The API recognises the following keys:
| Variable | Description | Default |
|---|---|---|
PORT |
Port for the Express server. | 3000 |
DATABASE_URL |
Full PostgreSQL connection string. If provided it takes precedence over the individual DB_* settings. | none |
DB_HOST |
Database host for local/dev use. | localhost |
DB_PORT |
Database port. | 5432 |
DB_USER |
Database user. | myuser |
DB_PASSWORD |
Database password. | mypassword |
DB_NAME |
Database name. | mydatabase |
DB_SSL |
Set to true to enable SSL connections (uses rejectUnauthorized: false). |
false |
JWT_SECRET |
Secret used for signing and verifying JWTs. Change this in production. | dev-secret-change-me |
Production deployments should provide secure values for these variables via the hosting platform's secret management system instead of storing them in .env.
| Method & Path | Description |
|---|---|
GET /api/products?gender=male|female|unisex |
List products, optionally filtered by gender. |
GET /api/categories |
Fetch all product categories. |
GET /api/search?q=<term> |
Search products by name or description. |
POST /api/register |
Create a new user account. Expects { "email": string, "password": string }. |
POST /api/login |
Authenticate a user. Returns { token } on success. |
GET /api/profile |
Get the authenticated user profile. Requires x-auth-token header. |
GET /api/cart |
Retrieve the authenticated user's cart. Requires x-auth-token. |
POST /api/cart |
Add/update an item in the cart. Body: { "productId": string, "quantity": number }. Requires x-auth-token. |
PUT /api/cart/item/:productId |
Set the quantity for a cart item. Body: { "quantity": number }. Requires x-auth-token. |
DELETE /api/cart/item/:productId |
Remove a product from the cart. Requires x-auth-token. |
npm install– install dependenciesnode setup-database.js– initialise or reset the databasenode server.js– start the API servernpm test– run the Jest test suite against the API endpoints
- For local Android emulator testing, expose the API using the host machine's IP address or a tunnelling service such as ngrok.