This backend API is built using Node.js and Express.js. It provides the necessary endpoints for managing users, including adding, viewing, updating, and deleting users. Deleted users are marked as Inactive
instead of being permanently removed.
-
User CRUD Operations
- Add User: Create a new user with required fields.
- View Users: Fetch all users or a single user by ID.
- Update User: Update user details, including status.
- Delete User: Mark a user as
Inactive
instead of permanent deletion.
-
Field Validation
- Required Fields:
Name
,Email
, andRole
. - Email Validation: Ensures a valid email format.
- Required Fields:
-
User Status Management
- Users have a
Status
field (Active
/Inactive
).
- Users have a
- Node.js
- MongoDB (Ensure MongoDB service is running locally or remotely)
-
Clone the repository:
git clone <repository-url> cd user-management-backend
-
Install dependencies:
npm install
-
Configure environment variables: Create a
.env
file in the root directory and configure the following variables:MONGO_URI=mongodb://<username>:<password>@<host>:<port>/<database> PORT=3000
-
Start the server:
npm start
The server will run on
http://localhost:3000
by default.
- URL:
/api/users/add
- Method:
POST
- Description: Add a new user.
- Request Body:
{ "name": "Chandan Kumar", "email": "chandan@gmail.com", "role": "Admin" }
- Response:
- 201: User created successfully.
- 400: Missing required fields.
- 500: Server error.
- URL:
/api/users/list
- Method:
GET
- Description: Fetch all users.
- Response:
- 200: List of users.
- 500: Server error.
- URL:
/api/users/list/:id
- Method:
GET
- Description: Fetch a single user by their ID.
- Response:
- 200: User details.
- 404: User not found.
- 500: Server error.
- URL:
/api/users/update/:id
- Method:
PUT
- Description: Update user details.
- Request Body:
{ "name": "Chandan Kumar", "email": "c@gmail.com", "role": "Editor" }
- Response:
- 200: User updated successfully.
- 404: User not found.
- 500: Server error.
- URL:
/api/users/delete/:id
- Method:
DELETE
- Description: Mark a user as
Inactive
. - Response:
- 200: User status updated to
Inactive
. - 404: User not found.
- 500: Server error.
- 200: User status updated to
- URL:
/api/users/update-status/:id
- Method:
PUT
- Description: Change the status of a user to
Inactive
. - Response:
- 200: User status updated successfully.
- 404: User not found.
- 500: Server error.
project-root/
models/
user.js # User schema definition
routes/
userRoutes.js # API routes
.env # Environment variables
index.js # Server entry point
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
match: [/.+@.+\..+/, 'Please enter a valid email'],
},
role: {
type: String,
required: true,
enum: ['Admin', 'Editor', 'Viewer'],
},
status: {
type: String,
default: 'Active',
enum: ['Active', 'Inactive'],
},
}, { timestamps: true });
module.exports = mongoose.model('User', userSchema);
- 400: Bad Request (e.g., missing or invalid fields).
- 404: Resource not found (e.g., user not found).
- 500: Internal Server Error.
- Add authentication and authorization.
- Implement logging and monitoring.
- Add unit and integration tests.
This project is licensed under the MIT License.