Skip to content

Latest commit

 

History

History
222 lines (160 loc) · 4.55 KB

File metadata and controls

222 lines (160 loc) · 4.55 KB

User Management API

A RESTful API built with Node.js, Express, and MongoDB for managing user authentication and CRUD operations.

🚀 Features

  • User Authentication: Secure sign-in with bcrypt password hashing
  • CRUD Operations: Complete user management (Create, Read, Update, Delete)
  • Bulk Operations: Support for creating multiple users at once
  • Password Security: Automatic password hashing on creation and updates
  • Input Validation: User data validation middleware
  • MongoDB Integration: Mongoose ODM for database operations
  • Modern ES Modules: Uses ES6+ module syntax (.mjs)

📋 Prerequisites

  • Node.js (v14 or higher)
  • MongoDB (local or cloud instance)
  • npm or yarn package manager

🛠️ Installation

  1. Clone the repository:
git clone <repository-url>
cd nodeapp
  1. Install dependencies:
npm install
  1. Create a .env file in the root directory:
PORT=3000
MONGO_URI=mongodb://localhost:27017/your-database-name

🏃 Running the Application

Development mode (with auto-reload):

npm start

The server will start on the port specified in your .env file.

📚 API Endpoints

Authentication

Sign In

POST /signin
Content-Type: application/json

{
  "email": "user@example.com",
  "password": "your-password"
}

Responses:

  • 200 OK - Successful login
  • 401 Unauthorized - Invalid password
  • 404 Not Found - User not found

User Management

Create User

POST /users
Content-Type: application/json

{
  "name": "John Doe",
  "email": "john@example.com",
  "age": 25,
  "password": "password123"
}

Response: 201 Created

Get All Users

GET /users

Response: 200 OK - Returns array of all users

Get User by ID

GET /users/:id

Response:

  • 200 OK - Returns user data
  • 400 Bad Request - User not found

Update User

PUT /users/:id
Content-Type: application/json

{
  "name": "Jane Doe",
  "email": "jane@example.com",
  "age": 26
}

Response:

  • 203 Non-Authoritative Information - User updated successfully
  • 404 Not Found - User not found

Delete User

DELETE /users/:id

Response:

  • 204 No Content - User deleted successfully
  • 404 Not Found - User not found

Bulk Create Users

POST /users/bulk
Content-Type: application/json

[
  {
    "name": "User 1",
    "email": "user1@example.com",
    "password": "password123"
  },
  {
    "name": "User 2",
    "email": "user2@example.com",
    "password": "password456"
  }
]

Response: 201 Created - Returns array of created users

🗂️ Project Structure

nodeapp/
├── models/
│   └── user.mjs           # User schema with password hashing
├── routes/
│   ├── router.signin.mjs  # Authentication routes
│   └── routes.user.mjs    # User CRUD routes
├── utils/
│   └── validateUser.mjs   # Input validation utility
├── db.mjs                 # Database connection configuration
├── server.mjs             # Main application entry point
├── package.json           # Project dependencies
└── .env                   # Environment variables (create this)

🔒 Security Features

  • Password Hashing: All passwords are hashed using bcrypt with a salt round of 10
  • Automatic Hashing: Passwords are hashed on:
    • User creation
    • User updates
    • Bulk insertions
  • Validation: Email uniqueness and password minimum length (8 characters)

📦 Dependencies

  • express - Web framework
  • mongoose - MongoDB ODM
  • bcrypt - Password hashing
  • dotenv - Environment variable management
  • chalk - Terminal output styling
  • nodemon - Development auto-reload

🔧 User Schema

Field Type Required Constraints
name String Yes Trimmed
email String Yes Unique, lowercase
age Number No Minimum: 0
password String Yes Unique, min length: 8
timestamps Date Auto createdAt, updatedAt

🤝 Contributing

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

👤 Author

Lalith Kumar

📞 Support

For support, email Lalith22p3347@gmail.com or open an issue in the repository.