✅ Authentication - JWT + Session Perfect
✅ Authorization - ACL
Api_v2_express/
├── app.js # Express app configuration
├── server.js # Server entry point
├── package.json # Dependencies
├── .env # Environment variables
│
├── src/ # 💻 Source Code
│ ├── config/ # Configuration (7 files)
│ ├── controllers/ # Controllers (24 files)
│ ├── middleware/ # Middleware (14 files)
│ ├── models/ # Mongoose models (28 files)
│ ├── routes/ # API routes (23 files)
│ ├── services/ # Business logic (2 files)
│ ├── utils/ # Utilities (11 files)
│ ├── validators/ # Validation schemas (1 file)
│ └── constants/ # Constants (1 file)
│
├── scripts/ # Utility scripts (9 files)
├── public/ # Static files
├── uploads/ # File uploads
└── json/ # JSON data files
npm installCreate .env file:
# Server
PORT=5000
NODE_ENV=development
# Database
MONGODB_URI=mongodb://localhost:27017/posnicpro
# JWT
JWT_SECRET=your-super-secret-key-min-32-characters
JWT_EXPIRES_IN=7d
JWT_COOKIE_EXPIRES_IN=7
# Session
SESSION_SECRET=your-session-secret-key
# CORS
CORS_ORIGIN=http://localhost:3000# Development
npm run dev
# Production
npm startAPI Base URL: http://localhost:5000/api
POST /api/users/verify
Content-Type: application/json
{
"username": "your-username",
"password": "your-password"
}
# Returns JWT token# Option 1: Header
GET /api/items
Authorization: Bearer YOUR_JWT_TOKEN
# Option 2: Cookie (automatic after login)
GET /api/items
Cookie: jwt=YOUR_JWT_TOKENAll responses follow PHP-compatible format:
{
"type": "success",
"message": "Items retrieved successfully",
"data": {
"list": [...],
"total": 100,
"page": 1,
"limit": 10
}
}{
"type": "error",
"message": "Unauthorized",
"data": null
}{
"item": { "read": true, "write": true, "delete": false },
"user": { "read": true, "write": false, "delete": false },
"sales": { "read": true, "write": true, "delete": false },
"report": { "read": true, "write": false, "delete": false }
// ... etc
}super_admin- Full access to everythingadmin- Full access to everythingmanager- Full access to everythingapi- Full access to everything
Controllers automatically check permissions using:
if (!this.checkPermission('item', 'read', req.user)) {
return this.error(res, 'Unauthorized', 403);
}{
"start": "node server.js",
"dev": "nodemon server.js",
"test": "jest",
"lint": "eslint .",
"format": "prettier --write ."
}- ✅ ES6+ JavaScript
- ✅ Async/await for async operations
- ✅ Error handling with try-catch
- ✅ Consistent response format
- ✅ JWT authentication
- ✅ MongoDB/Mongoose ORM
- ✅ Express.js best practices
All configuration in src/config/:
config.js- Main configurationdatabase.js- MongoDB connectionauth.js- Authentication configenvironment.js- Environment settingstokens.js- Token typesroles.js- User roles
- express - Web framework
- mongoose - MongoDB ODM
- jsonwebtoken - JWT authentication
- bcryptjs - Password hashing
- helmet - Security headers
- cors - CORS middleware
- express-validator - Input validation
- pdfkit - PDF generation
- exceljs - Excel generation
- node-cron - Scheduled tasks
- Set
NODE_ENV=production - Configure production MongoDB URI
- Set strong JWT_SECRET (32+ chars)
- Enable HTTPS
- Configure CORS for production domain
- Environment variables configured
- MongoDB connection tested
- SSL/TLS enabled
- JWT secret is strong
- CORS configured for production
- Error logging enabled
- Rate limiting configured
- File upload limits set
- Backup strategy in place
✅ Connection pooling (MongoDB)
✅ Response compression
✅ Rate limiting
✅ Input sanitization
✅ XSS protection
✅ NoSQL injection prevention
✅ Efficient queries with indexes
✅ Stateless JWT authentication
- ✅ Helmet security headers
- ✅ CORS protection
- ✅ Rate limiting
- ✅ XSS protection
- ✅ NoSQL injection prevention
- ✅ JWT authentication
- ✅ Password hashing (bcrypt)
- ✅ Input validation & sanitization
- ✅ HTTP-only cookies
- ✅ Secure cookies in production
MongoDB Connection Failed
# Check MongoDB is running
mongosh
# Verify connection string in .env
MONGODB_URI=mongodb://localhost:27017/posnicproJWT Token Invalid
# Check JWT_SECRET is set
# Check token expiration
# Re-login to get new tokenCORS Errors
# Configure CORS_ORIGIN in .env
CORS_ORIGIN=http://localhost:3000