This project is a RESTful API for managing courses, built with Node.js, Express.js, and MongoDB. It provides CRUD (Create, Read, Update, Delete) operations for course management.
Before you begin, ensure you have met the following requirements:
- You have installed Node.js (version 12.x or later) and npm. You can download them from nodejs.org.
- You have installed MongoDB (version 4.x or later). You can download it from mongodb.com.
- You have a GitHub account and Git installed on your local machine.
- You have a basic understanding of JavaScript and RESTful APIs.
To clone this project, follow these steps:
-
Open your terminal (Command Prompt on Windows, Terminal on macOS or Linux).
-
Navigate to the directory where you want to clone the project.
-
Run the following command:
git clone https://github.com/TheMarvelFan/Course-Api.git
-
Navigate into the project directory:
cd Course-Api
After cloning the project, you need to set it up:
-
Install the project dependencies:
npm install
-
Create a
.envfile in the root directory of the project:touch .env
-
Open the
.envfile in a text editor and add the following environment variables:MONGODB_URI=your_mongodb_connection_string_here PORT=3000Replace
your_mongodb_connection_string_herewith your actual MongoDB connection string.
To run the project, follow these steps:
-
Make sure MongoDB is running on your machine.
-
Start the server:
For production:
npm start
For development (with auto-restart on file changes):
npm run dev
-
The server should now be running on
http://localhost:3000(or the port you specified in the.envfile).
You can test the API using tools like Postman or curl. Here are the available endpoints:
- POST
/api/courses: Create a new course - GET
/api/courses: Retrieve all courses - GET
/api/courses/:id: Retrieve a specific course by ID - PUT
/api/courses/:id: Update a course by ID - DELETE
/api/courses/:id: Delete a course by ID
Example of creating a new course using curl:
curl -X POST -H "Content-Type: application/json" -d '{"title":"Introduction to Node.js","description":"Learn the basics of Node.js","duration":120}' http://localhost:3000/api/courses-
Database Connection: If you're using a different MongoDB setup (like a remote database), make sure to update the
MONGODB_URIin your.envfile accordingly. -
Port: The default port is set to 3000. If you need to use a different port, you can change it in the
.envfile. -
Course Model: If you need to add or modify fields for the course model, you can do so in the
models/Course.jsfile. Remember to update the corresponding route handlers inroutes/courses.jsif you make changes to the model. -
Error Handling: The current implementation includes basic error handling. For a production environment, you might want to implement more robust error handling and logging.
-
Authentication: This API doesn't include authentication. For a production application, consider implementing user authentication and authorization.
-
Validation: You may want to add more detailed validation for course data. Consider using a library like
express-validatorfor this purpose.
- POST
/api/courses:
- GET
/api/courses:
- GET
/api/courses/:id:
-
PUT
/api/courses/:id: https://github.com/user-attachments/assets/3f7fae07-9e3d-4af2-9738-28fa8ebdd86e -
DELETE
/api/courses/:id: https://github.com/user-attachments/assets/3ddf9e1a-7bb6-40f2-8cc6-89b62fd88ecc


