A RESTful API built with Node.js, Express, and MongoDB for managing user authentication and CRUD operations.
- 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)
- Node.js (v14 or higher)
- MongoDB (local or cloud instance)
- npm or yarn package manager
- Clone the repository:
git clone <repository-url>
cd nodeapp- Install dependencies:
npm install- Create a
.envfile in the root directory:
PORT=3000
MONGO_URI=mongodb://localhost:27017/your-database-namenpm startThe server will start on the port specified in your .env file.
POST /signin
Content-Type: application/json
{
"email": "user@example.com",
"password": "your-password"
}Responses:
200 OK- Successful login401 Unauthorized- Invalid password404 Not Found- User not found
POST /users
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com",
"age": 25,
"password": "password123"
}Response: 201 Created
GET /usersResponse: 200 OK - Returns array of all users
GET /users/:idResponse:
200 OK- Returns user data400 Bad Request- User not found
PUT /users/:id
Content-Type: application/json
{
"name": "Jane Doe",
"email": "jane@example.com",
"age": 26
}Response:
203 Non-Authoritative Information- User updated successfully404 Not Found- User not found
DELETE /users/:idResponse:
204 No Content- User deleted successfully404 Not Found- User not found
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
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)
- 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)
- express - Web framework
- mongoose - MongoDB ODM
- bcrypt - Password hashing
- dotenv - Environment variable management
- chalk - Terminal output styling
- nodemon - Development auto-reload
| Field | Type | Required | Constraints |
|---|---|---|---|
| name | String | Yes | Trimmed |
| String | Yes | Unique, lowercase | |
| age | Number | No | Minimum: 0 |
| password | String | Yes | Unique, min length: 8 |
| timestamps | Date | Auto | createdAt, updatedAt |
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Lalith Kumar
For support, email Lalith22p3347@gmail.com or open an issue in the repository.