A simple Express.js backend for inventory management with role-based authentication.
- Authentication System: JWT-based authentication with user registration and login
- Role-Based Access Control: Admin and user roles with different permissions
- Product Management: CRUD operations for products with role-based restrictions
- SQLite Database: Lightweight database for data persistence
- Input Validation: Request validation using express-validator
- CORS Support: Configured for frontend integration
- Can create, read, update, and delete products
- Can manage user accounts
- Full access to all features
- Can only read/view products
- Cannot modify or delete products
- Limited access to features
- Node.js (v14 or higher)
- npm or yarn
- Navigate to the backend directory:
cd inventory-app-backend- Install dependencies:
npm install-
Configure environment variables:
- Copy
config.envand modify as needed - Change the JWT_SECRET for production
- Copy
-
Start the server:
# Development mode (with auto-restart)
npm run dev
# Production mode
npm startThe server will start on http://localhost:5000
A default admin account is automatically created:
- Username: admin
- Password: admin123
Important: Change these credentials in production!
POST /api/auth/register
Content-Type: application/json
{
"username": "newuser",
"email": "user@example.com",
"password": "password123",
"role": "user" // optional, defaults to "user"
}
POST /api/auth/login
Content-Type: application/json
{
"username": "admin",
"password": "admin123"
}
GET /api/auth/profile
Authorization: Bearer <token>
PUT /api/auth/profile
Authorization: Bearer <token>
Content-Type: application/json
{
"email": "newemail@example.com",
"password": "newpassword123"
}
GET /api/products
Authorization: Bearer <token>
GET /api/products/:id
Authorization: Bearer <token>
POST /api/products
Authorization: Bearer <token>
Content-Type: application/json
{
"name": "Product Name",
"description": "Product description",
"price": 29.99,
"quantity": 100,
"category": "Electronics"
}
PUT /api/products/:id
Authorization: Bearer <token>
Content-Type: application/json
{
"name": "Updated Product Name",
"price": 39.99,
"quantity": 50
}
DELETE /api/products/:id
Authorization: Bearer <token>
GET /api/products/search/:query
Authorization: Bearer <token>
GET /api/health
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
email TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
role TEXT NOT NULL DEFAULT 'user',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);CREATE TABLE products (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
description TEXT,
price REAL NOT NULL,
quantity INTEGER NOT NULL DEFAULT 0,
category TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);The API returns consistent error responses:
{
"message": "Error description"
}For validation errors:
{
"errors": [
{
"msg": "Validation error message",
"param": "field_name",
"location": "body"
}
]
}- Password Hashing: Passwords are hashed using bcrypt
- JWT Tokens: Secure token-based authentication
- Input Validation: All inputs are validated and sanitized
- Role-Based Access: Different permissions for different user roles
- CORS Protection: Configured CORS for frontend integration
inventory-app-backend/
├── database.js # Database setup and initialization
├── server.js # Main server file
├── config.env # Environment variables
├── package.json # Dependencies and scripts
├── middleware/
│ └── auth.js # Authentication middleware
├── routes/
│ ├── auth.js # Authentication routes
│ └── products.js # Product management routes
└── README.md # This file
- Create new route files in the
routes/directory - Add middleware in the
middleware/directory if needed - Register new routes in
server.js - Update this README with new endpoints
- Environment Variables: Use proper environment variables for sensitive data
- JWT Secret: Use a strong, unique JWT secret
- Database: Consider using a production database like PostgreSQL
- HTTPS: Enable HTTPS in production
- Rate Limiting: Implement rate limiting for API endpoints
- Logging: Add proper logging for production monitoring
- Backup: Implement database backup strategies
ISC