This Express.js backend API provides endpoints for performing CRUD (Create, Read, Update, Delete) operations on MongoDB.
-
Clone this repository:
git clone https://github.com/DimitarAtanassov/mongodb-crud-api.git
-
Install dependencies:
npm install
-
Set up your MongoDB connection by creating a
.env
file in the root directory and adding your MongoDB connection string:MONGODB_URL=your_mongodb_connection_string
-
Start the server:
npm start
-
Access the API endpoints using Postman or another HTTP client:
GET /api/users
: Retrieve all UsersGET /api/users/:id
: Retrieve a User by IDPOST /api/users
: Create a new UserPUT /api/users/:id
: Update a User by IDDELETE /api/users/:id
: Delete a User by ID
To run this project, you will need to add the following environment variables to your .env
file:
MONGODB_URL
: MongoDB connection string
POST /api/users
: Create a new userPOST /api/users/login
: User loginPUT /api/users/:userID
: Update user password (Protected)DELETE /api/users/:userID
: Delete a user (Protected)GET /api/users/:userID
: Get user by ID (Protected)
POST /api/users/jobapps
: Create a job application (Protected)GET /api/jobapps
: Get all job applications by user ID (Protected)PUT /api/jobapps/:id/status
: Update job application status (Protected)
verifyToken
: Verifies JWT included in the request header
username
: String (required, unique)email
: String (required, unique)password
: String (required)jobApplications
: Array of ObjectIDs referencing JobApplication documents
company
: String (required)title
: String (required)link
: String (required)user
: ObjectID referencing a User document (required)status
: String (default: 'pending', enum: ['pending', 'accepted', 'rejected'])
user.js
: CRUD functions for User documents in MongoDBjobApp.js
: CRUD functions for Job Application documents in MongoDB
auth.js
: User authentication middleware using JSON Web Token (JWT)validators.js
: Utility functions for input validation