This is a Node.js/Express backend for an African food marketplace application. Let me walk you through the key components:
The codebase follows a modular architecture with clear separation of concerns:
-
Auth Module: Handles user authentication and authorization
- JWT-based authentication
- Role-based access control (admin, student, instructor roles)
- Password hashing using bcryptjs
-
Items Module: Manages food items/products
- CRUD operations for food items
- Each item has title, price, quantity, and image URL
-
Users Module: Handles user management
- User creation and retrieval
- Role management
- Uses transactions for data integrity when creating users
- Separates business logic from routes
- Handles error responses and success states
- Currently implements items-related operations
- Uses Knex.js as the query builder
- Configurable for different environments (development, testing, production)
- Supports both PostgreSQL and SQLite databases
Two main tables:
-
Users/Roles Tables (20220308130032_initial-migration.js)
- Many-to-one relationship between users and roles
- Role-based authorization system
-
Items Table (20211115082418_items.js)
- Stores food items with properties like:
- Title (unique)
- Price
- Quantity
- Image URL
- Stores food items with properties like:
- Includes cleanup utility
- Populates initial roles (admin, student, instructor)
- Creates test users
- Adds sample food items (African dishes like Jollof Rice)
-
Security
- JWT authentication
- Password hashing
- Role-based access control
- Helmet for HTTP security headers
-
Database
- Knex migrations for version control
- Transaction support
- Relationship management
- Data seeding
-
API Design
- RESTful endpoints
- Middleware for authentication and validation
- Error handling
- Response standardization
- Environment configuration using dotenv
- Hot reloading with nodemon
- Database migration and seeding scripts
- ESLint for code quality
This architecture allows for scalability, maintainability, and separation of concerns while providing a secure and efficient API for the frontend application.
- Node.js (v12 or higher)
- npm or yarn
- PostgreSQL (for production) or SQLite (for development)
- Clone the repository
git clone https://github.com/Afri-Mart/Backend.git
cd Backend
- Install dependencies
npm install
- Set up environment variables
Create a
.env
file in the root directory:
PORT=5001
JWT_SECRET=your_secret_here
NODE_ENV=development
- Set up the database
# Run migrations to create database tables
npm run migrate
# Seed the database with initial data
npm run seed
- Development mode (with hot reload)
npm run server
- Production mode
npm start
The server will start on http://localhost:5001
npm start
- Runs the server in production modenpm run server
- Runs the server with nodemon for developmentnpm run migrate
- Runs database migrationsnpm run rollback
- Rolls back the last migrationnpm run seed
- Seeds the database with initial datanpm run reset
- Rolls back, migrates, and seeds the database (full reset)
# Register a new user
POST http://localhost:5001/api/auth/register
{
"username": "testuser",
"password": "password123",
"role_name": "student"
}
# Login
POST http://localhost:5001/api/auth/login
{
"username": "testuser",
"password": "password123"
}
# Get all items
GET http://localhost:5001/api/items
# Get specific item
GET http://localhost:5001/api/items/:id
# Get all users (requires auth token)
GET http://localhost:5001/api/users
# Get specific user (requires admin role)
GET http://localhost:5001/api/users/:user_id
If you need to reset the database to its initial state:
npm run reset
- Port Already in Use
If you see
EADDRINUSE
error:
# Change the port in package.json scripts
"start": "PORT=5002 node index.js",
"server": "PORT=5002 nodemon index.js"
- Database Connection Issues
- Ensure PostgreSQL is running (if using PostgreSQL)
- Check database credentials in knexfile.js
- For development, SQLite database will be created automatically
├── api/
│ ├── auth/ # Authentication related files
│ ├── items/ # Items related files
│ ├── users/ # Users related files
│ └── secrets/ # Environment secrets
├── controllers/ # Business logic
├── data/ # Database configuration
├── migrations/ # Database migrations
├── seeds/ # Seed data
└── package.json # Project dependencies and scripts
The application is ready for deployment on platforms like Heroku, AWS, or any Node.js hosting service. Make sure to:
- Set appropriate environment variables
- Configure the database connection
- Run migrations on deployment
- Set up proper security measures (HTTPS, etc.)
