Vineet Kumar profile picture

Vineet Kumar

+1 437-428-2199 | +1 301-979-5562  |  contactkrvineet@gmail.com

Rest API - Express Service Boilerplate

Deploy Status

A RESTful API built with Node.js, Express, and MongoDB Atlas for managing user data. This project demonstrates modern API development practices with proper validation, error handling, and cloud deployment.

Live API: https://api.vineetkr.com


📚 Table of Contents


🛠️ Technology Stack

Backend Framework Database Validation & Security Development Tools Deployment

🚀 What is Express.js?

Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for building web and mobile applications. It's the de facto standard server framework for Node.js.

Why Express.js?
  1. Fast & Lightweight - Minimal overhead with high performance
  2. Easy to Learn - Simple, intuitive API design
  3. Middleware Support - Extensible through middleware functions
  4. Robust Routing - Powerful routing mechanism for handling HTTP requests
  5. Large Ecosystem - Thousands of npm packages compatible with Express
How Express Creates APIs
// 1. Import Express
import express from "express";
const app = express();

// 2. Define Routes (API Endpoints)
app.get("/api/users", (req, res) => {
  res.json({ message: "Get all users" });
});

app.post("/api/users", (req, res) => {
  res.json({ message: "Create user" });
});

// 3. Start Server
app.listen(3000, () => {
  console.log("API running on port 3000");
});
Key Concepts:

✨ Project Features


📁 Project Structure

vineetkr-api/
├── src/
│   ├── server.js              # Express app setup & entry point
│   ├── config/
│   │   └── database.js        # MongoDB connection config
│   ├── controllers/
│   │   └── userController.js  # Business logic for user operations
│   ├── models/
│   │   └── User.js            # Mongoose schema & model
│   ├── routes/
│   │   └── userRoutes.js      # API route definitions
│   └── validators/
│       └── userValidator.js   # Zod validation schemas
├── .github/
│   └── workflows/
│       └── deploy.yml         # GitHub Actions CI/CD
├── .env                       # Local environment variables (gitignored)
├── .env.example               # Template for environment variables
├── .gitignore                 # Git ignore rules
├── vercel.json                # Vercel deployment config
├── package.json               # Dependencies & scripts
└── README.md                  # This file
Architecture Flow
Request → Express Middleware → Router → Controller → Model → Database
                                                              ↓
Response ← JSON Response ← Controller ← Model ← Database Query

🔌 API Endpoints

Base URL: https://api.vineetkr.com

Get All Users
GET /api/users
Response:
{
  "success": true,
  "count": 2,
  "data": [
    {
      "_id": "507f1f77bcf86cd799439011",
      "name": "Vineet Kumar",
      "email": "vineet@vineetkr.com",
      "age": 25,
      "createdAt": "2025-12-25T10:30:00.000Z",
      "updatedAt": "2025-12-25T10:30:00.000Z"
    }
  ]
}
Get Single User
GET /api/users?id=507f1f77bcf86cd799439011
Create User
POST /api/users
Content-Type: application/json

{
  "name": "John Doe",
  "email": "john@example.com",
  "age": 30
}
Response:
{
  "success": true,
  "message": "User created successfully",
  "data": {
    "_id": "507f1f77bcf86cd799439011",
    "name": "John Doe",
    "email": "john@example.com",
    "age": 30
  }
}
Update User
PUT /api/users/:id
Content-Type: application/json

{
  "name": "Jane Doe",
  "age": 32
}

🏁 Getting Started

Prerequisites Installation
  1. Clone the repository
    git clone https://github.com/contactkrvineet/vineetkr-api.git
    cd vineetkr-api
    
  2. Install dependencies
    npm install
    
  3. Set up environment variables
    # Copy example file
    cp .env.example .env
    
    # Edit .env and add your credentials
    nano .env
    
  4. Configure MongoDB Atlas
    • Create a cluster at MongoDB Atlas
    • Get your connection string:
      • Click ConnectConnect your application
      • Copy the connection string
    • Update Network Access:
      • Go to Network AccessAdd IP Address
      • Allow access from anywhere: 0.0.0.0/0 (for development)
  5. Update .env file
    PORT=3000
    MONGODB_URI=mongodb+srv://credluster0.xxxxx.mongodb.net/your_database
    NODE_ENV=development
    ALLOWED_ORIGINS=http://localhost:3000
    
Running the Application

The API will be running at: http://localhost:3000

Testing the API

🔐 Environment Variables

Required Variables
Variable Description Example
PORT Server port number 3000
MONGODB_URI MongoDB connection string mongodb+srv://@cluster.mongodb.net/db
NODE_ENV Environment mode development or production
ALLOWED_ORIGINS CORS allowed origins (comma-separated) https://vineetkr.com,http://localhost:3000
Local Development
PORT=3000
MONGODB_URI=mongodb+srv://@cluster.mongodb.net/database
NODE_ENV=development
ALLOWED_ORIGINS=http://localhost:3000
Production (Vercel)
  1. Go to Project SettingsEnvironment Variables
  2. Add each variable with production values
  3. Redeploy the application

🚢 Deployment

Deploy to Vercel
  1. Install Vercel CLI
    npm install -g vercel
    
  2. Login to Vercel
    vercel login
    
  3. Deploy
    vercel --prod
    
  4. Set Environment Variables
    • Go to Vercel dashboard
    • Navigate to SettingsEnvironment Variables
    • Add all required variables
    • Redeploy
Automatic Deployment (GitHub Actions)

This project includes a GitHub Actions workflow that automatically deploys to Vercel on every push to main or prac branch.

Setup:
  1. Add secrets to GitHub repository:
    • VERCEL_TOKEN - Get from vercel.com/account/tokens
    • VERCEL_ORG_ID - Found in .vercel/project.json
    • VERCEL_PROJECT_ID - Found in .vercel/project.json
  2. Push to main or prac branch - deployment happens automatically!

🔒 Security

Best Practices Implemented Security Checklist

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📝 License

This project is licensed under the Vineetkr License.


👨‍💻 Author

Vineet Kumar
Website: vineetkr.com
GitHub: @contactkrvineet


📞 Support


🎓 Learning Resources

Happy Coding! 🚀