A professional-grade REST API built with MVC architecture, Service Layer pattern, Express.js, Sequelize ORM, and PostgreSQL.
π£οΈ Find complete postman collection in the postman folder. Import the Sequelize Practice.postman_collection.json into your postman for testing.
Clean Architecture with Separation of Concerns:
π Controllers β Handle HTTP requests/responses
π§ Services β Business logic and data operations
ποΈ Models β Database models and associations
π‘οΈ Middleware β Error handling and validation
π£οΈ Routes β API endpoint definitions
βοΈ Config β Database and application configuration
-
Start the server:
npm run dev
-
Initialize database (first time):
npm run db:init npm run db:seed
-
Test the API:
npm run test:api
src/
βββ config/
β βββ connection.js # Database connection setup
β βββ database.js # Database configuration
βββ controllers/
β βββ userController.js # User HTTP handlers
β βββ postController.js # Post HTTP handlers
β βββ commentController.js# Comment HTTP handlers
β βββ profileController.js# Profile HTTP handlers
βββ services/
β βββ userService.js # User business logic
β βββ postService.js # Post business logic
β βββ commentService.js # Comment business logic
β βββ profileService.js # Profile business logic
βββ models/
β βββ index.js # Model loader and associations
β βββ User.js # User model
β βββ Post.js # Post model
β βββ Comment.js # Comment model
β βββ Profile.js # Profile model
βββ routes/
β βββ users.js # User API routes
β βββ posts.js # Post API routes
β βββ comments.js # Comment API routes
β βββ profiles.js # Profile API routes
βββ middleware/
βββ errorHandler.js # Centralized error handling
| Script | Description |
|---|---|
npm run dev |
Start server with auto-reload |
npm start |
Start production server |
npm run console |
Run Sequelize console mode |
npm run test:api |
Run comprehensive API tests |
npm run db:init |
Initialize database tables |
npm run db:seed |
Seed with sample data |
npm run db:reset |
GET /api/users # List users (paginated, filtered)
GET /api/users/:id # Get user by ID
POST /api/users # Create user
PUT /api/users/:id # Update user
DELETE /api/users/:id # Delete user
GET /api/users/:id/posts # Get user's posts
POST /api/users/:id/posts # Create post for userGET /api/posts # List posts (paginated, filtered)
GET /api/posts/published # Get published posts
GET /api/posts/:id # Get post by ID
GET /api/posts/slug/:slug # Get post by slug
POST /api/posts # Create post
PUT /api/posts/:id # Update post
PUT /api/posts/:id/publish # Publish post
DELETE /api/posts/:id # Delete post
GET /api/posts/:id/comments # Get post comments
POST /api/posts/:id/comments # Add comment to postGET /api/comments # List comments (paginated, filtered)
GET /api/comments/approved # Get approved comments
GET /api/comments/pending # Get pending comments
GET /api/comments/:id # Get comment by ID
POST /api/comments # Create comment
PUT /api/comments/:id # Update comment
PUT /api/comments/:id/approve # Approve comment
DELETE /api/comments/:id # Delete commentGET /api/profiles # List profiles
GET /api/profiles/:id # Get profile by ID
GET /api/profiles/user/:userId # Get profile by user ID
POST /api/profiles # Create profile
PUT /api/profiles/:id # Update profile
PUT /api/profiles/:id/preferences # Update preferences
PUT /api/profiles/:id/social-media # Add social media
DELETE /api/profiles/:id # Delete profile- β Handle HTTP requests/responses
- β Input validation and sanitization
- β Response formatting
- β Error handling delegation
- β Business logic encapsulation
- β Data transformation
- β Multiple data source coordination
- β Reusable across controllers
- β Database schema definition
- β Data validation rules
- β Relationships and associations
- β Query scopes and methods
- β Centralized error handling
- β Request/response processing
- β Authentication (ready for JWT)
- β Logging and monitoring
GET /api/users?page=2&limit=10GET /api/posts?status=published&search=nodejs&tag=tutorial
GET /api/users?isActive=true&search=john
GET /api/comments?isApproved=false&postId=123GET /api/users?includeProfile=true&includePosts=true
GET /api/posts?includeAuthor=true&includeComments=true GET /api/posts/published # Using published scope
GET /api/comments/pending # Using pending scope
GET /api/comments/approved # Using approved scope# 1. Create user
curl -X POST http://localhost:3000/api/users \
-H "Content-Type: application/json" \
-d '{"firstName":"John","lastName":"Doe","email":"john@example.com"}'
# 2. Create profile
curl -X POST http://localhost:3000/api/profiles \
-H "Content-Type: application/json" \
-d '{"userId":1,"bio":"Developer","location":"NYC"}'# 1. Create draft post
curl -X POST http://localhost:3000/api/posts \
-H "Content-Type: application/json" \
-d '{"title":"My Post","content":"Content...","slug":"my-post","userId":1}'
# 2. Publish post
curl -X PUT http://localhost:3000/api/posts/1/publish# 1. Create comment (starts as pending)
curl -X POST http://localhost:3000/api/comments \
-H "Content-Type: application/json" \
-d '{"content":"Great post!","userId":2,"postId":1}'
# 2. Approve comment
curl -X PUT http://localhost:3000/api/comments/1/approvenpm run test:api# Health check
curl http://localhost:3000/health
# Get users with profiles
curl "http://localhost:3000/api/users?includeProfile=true"
# Get published posts with authors
curl "http://localhost:3000/api/posts/published?includeAuthor=true"- β MVC architecture implementation
- β Service layer pattern
- β Middleware development
- β RESTful API design
- β Error handling strategies
- β Model definitions and validations
- β Associations and relationships
- β Query optimization
- β Scopes and instance methods
- β Hooks and lifecycle events
NODE_ENV=development
PORT=3000
DB_HOST=localhost
DB_PORT=5432
DB_NAME=sequelize_db
DB_USERNAME=postgres
DB_PASSWORD=postgres- Host: localhost:5432
- Database: sequelize_db
- Username: postgres
- Password: postgres
The API provides comprehensive error handling:
- 400 Bad Request - Validation errors, invalid input
- 404 Not Found - Resource doesn't exist
- 409 Conflict - Duplicate entries (email, slug)
- 500 Internal Server Error - Server-side errors
- Start with services - Write business logic first
- Keep controllers thin - Delegate to services
- Use transactions - For multi-model operations
- Leverage associations - Reduce N+1 queries
- Handle errors gracefully - Use middleware
- Test incrementally - Use
npm run test:api
Happy practicing with modern Node.js architecture!