Series Overview
This is Part 1 of a complete series where we’ll build a production-ready RESTful API from scratch. By the end, you’ll have:
- A fully functional REST API with CRUD operations
- User authentication with JWT
- MongoDB database integration
- Input validation and error handling
- API documentation with Swagger
- Comprehensive testing
- Docker containerization
- Deployment on Render/Railway
What We’re Building
A Task Management API that supports:
- User registration and authentication
- Creating, reading, updating, and deleting tasks
- Filtering and pagination
- Role-based access control
Prerequisites
- Node.js 18+ installed
- MongoDB account (we’ll use MongoDB Atlas free tier)
- Basic JavaScript knowledge
- Code editor (VS Code recommended)
Part 1: Project Setup and Foundation
Let’s get our development environment ready and create the project structure.
Step 1: Initialize Project
Create a new directory and initialize npm:
|
|
Step 2: Install Core Dependencies
|
|
Package purposes:
express: Web frameworkmongoose: MongoDB ODMdotenv: Environment variablescors: Cross-origin resource sharinghelmet: Security headers
Step 3: Install Development Dependencies
|
|
Update package.json scripts:
|
|
Step 4: Create Project Structure
|
|
Your structure should look like:
|
|
Step 5: Configure Environment Variables
Create .env file:
|
|
⚠️ We’ll update MONGODB_URI with Atlas connection string in Part 2.
Step 6: Setup .gitignore
Add to .gitignore:
|
|
Step 7: Create Basic Express App
src/app.js:
|
|
src/server.js:
|
|
Step 8: Test the Server
Run the development server:
|
|
You should see:
|
|
Test the health endpoint:
|
|
Expected response:
|
|
Step 9: Initialize Git Repository
|
|
What We’ve Accomplished
✅ Project initialized with proper structure
✅ Core dependencies installed
✅ Environment configuration ready
✅ Basic Express server running
✅ Security middleware configured
✅ Health check endpoint working
✅ Git repository initialized
Next in Part 2
In the next part, we’ll:
- Set up MongoDB Atlas
- Create database connection
- Define our first Mongoose model
- Implement basic CRUD operations
Common Issues
Port already in use
If port 5000 is occupied:
- Change
PORTin.envto another number (e.g., 3001) - Or kill the process using port 5000:
|
|
Module not found errors
Ensure you ran npm install and all dependencies are in package.json.
Full Code
You can find the complete code for Part 1 on GitHub: [link to be added]
Continue to: Part 2: Database Setup and Models (Coming Soon)
Series Index:
- ✅ Part 1: Project Setup (You are here)
- ⏳ Part 2: Database and Models
- ⏳ Part 3: Authentication
- ⏳ Part 4: Task CRUD Operations
- ⏳ Part 5: Testing
- ⏳ Part 6: Documentation
- ⏳ Part 7: Deployment