A production-ready Express.js REST API for managing products and users with JWT authentication, health monitoring, and Prometheus metrics.
- Quick Start
- Features
- API Endpoints
- Authentication
- Project Structure
- Environment Variables
- Technologies
- Node.js v18+
- npm or pnpm
git clone https://github.com/BenjaSar/backend_NodeJS.git
cd Project
npm installCreate a .env file:
PORT=3000
NODE_ENV=development
JWT_SECRET=your_jwt_secret_key
ALLOWED_ORIGINS=*npm startServer runs at http://localhost:3000/
- RESTful API with CRUD operations
- JWT token-based authentication
- Product management with stock tracking
- Health monitoring endpoint
- Prometheus metrics integration
- Comprehensive request logging (Winston)
- CORS support
- Centralized error handling
- Database integration (Firebase/MongoDB)
- Automatic restart on file changes (Nodemon)
| Method | Endpoint | Description | Auth |
|---|---|---|---|
| GET | / |
Welcome page | No |
| GET | /health |
Server health check | No |
| GET | /ping |
Ping endpoint | No |
| GET | /metrics |
Prometheus metrics | No |
| Method | Endpoint | Body | Auth |
|---|---|---|---|
| POST | /api/login |
{ "username", "password" } |
No |
Login Example:
curl -X POST http://localhost:3000/api/login \
-H "Content-Type: application/json" \
-d '{"username":"<your-username>","password":"<your-password>"}'Response:
{ "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." }| Method | Endpoint | Description | Auth |
|---|---|---|---|
| GET | /api/products |
Get all products | Yes |
| GET | /api/products/:id |
Get product by ID | Yes |
| GET | /api/products/in-stock |
Get in-stock products | Yes |
| POST | /api/products/create |
Create product | Yes |
| PATCH | /api/products/:id/stock |
Update stock | Yes |
| DELETE | /api/products/:id |
Delete product | Yes |
Create Product Example:
curl -X POST http://localhost:3000/api/products/create \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{
"name": "Product Name",
"price": 99.99,
"category": "electronics",
"stock": 50
}'Required Fields for Product Creation:
name- Product name (string)price- Product price (number)category- Product category (string)stock- Stock quantity (number)
All protected endpoints require a JWT token in the Authorization header:
Authorization: Bearer <your_jwt_token>
Default Test Credentials:
- Username:
<your-usenname> - Password:
<your-password>
- Login to get a token
- Use token in subsequent requests
Project/
├── index.js # Server entry point
├── package.json
├── .env # Environment variables
├── prometheus.yml # Prometheus config
├── src/
│ ├── controllers/ # Request handlers
│ ├── services/ # Business logic
│ ├── routes/ # API routes
│ ├── models/ # Data models
│ ├── middleware/ # JWT validation
│ ├── utils/ # Logger, utilities
│ ├── data/ # Static data
│ └── logs/ # Application logs
└── doc/ # Documentation assets
Architecture: Routes → Controllers → Services → Models
# Server
PORT=3000
NODE_ENV=development
ALLOWED_ORIGINS=*
# JWT
JWT_SECRET=your_super_secret_key
# Firebase (optional)
FIREBASE_API_KEY=key
FIREBASE_PROJECT_ID=project_id
FIREBASE_AUTH_DOMAIN=auth_domain
# MongoDB (optional)
MONGODB_URI=mongodb://localhost:27017/store- Express.js - Web framework
- JWT - Authentication
- Winston - Logging
- CORS - Cross-origin requests
- Prometheus - Metrics
- Nodemon - Dev server reload
- Firebase - Optional auth/database
- Mongoose - Optional MongoDB ODM
Module Not Found:
- Check import paths and file names
- Ensure correct directory structure
JWT Token Errors:
- Verify token in Authorization header
- Check JWT_SECRET is set in .env
- Ensure token hasn't expired
CORS Issues:
- Update ALLOWED_ORIGINS in .env
- Verify client sends proper headers
Product Creation Fails:
- Ensure all required fields: name, price, category, stock
- Check field values are correct types
- Verify Content-Type: application/json header
curl http://localhost:3000/healthResponse:
{
"uptime": 123.456,
"message": "OK",
"timestamp": 1700318000000
}- Fork repository
- Create feature branch:
git checkout -b feature/name - Commit changes:
git commit -m 'Add feature' - Push branch:
git push origin feature/name - Open Pull Request
MIT
FS - Backend Developer
Made with ❤️

