Skip to content

spm999/Restful_API_CRUD

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Node.js Express API with MongoDB: This is a simple Node.js API built using Express.js and MongoDB for managing tasks. You can use this README.md file as a guide to set up and run the application.

Prerequisites: Before you begin, ensure you have the following prerequisites installed on your system:

Node.js: Make sure you have Node.js installed. You can download it from the official website.

MongoDB: Install and set up MongoDB on your machine or use a cloud-based MongoDB service like MongoDB Atlas.

Visual Studio Code (VSCode): You can use any code editor of your choice, but this guide assumes you are using VSCode.

Getting Started: Clone or download this repository to your local machine.

Open a terminal and navigate to the project directory.

Install the required Node.js packages by running the following command:

npm install

Create a .env file in the project directory to store your MongoDB connection URI. Add the following line to the .env file, replacing and with your MongoDB username and password:

MONGO_URI=mongodb+srv://:@gallary.xducnkc.mongodb.net/Node?retryWrites=true&w=majority Note: If you prefer not to use a .env file, you can directly replace the MongoDB connection URI in the server.js file.

Open the server.js file and review the code to ensure it meets your requirements. You can customize the API routes and functionality as needed.

Start the Node.js application with the following command:

npm run dev This command uses nodemon to automatically restart the server when code changes are detected.

Once the server is running, you should see output in the terminal indicating that the app is running and connected to MongoDB:

[nodemon] starting node server.js App is running Connected to MongoDB

Using the API You can use Thunder Client in VSCode or any API testing tool of your choice to interact with the API endpoints. Here are some example requests:

GET Request: Retrieve a list of all tasks. http GET http://localhost:3000/tasks

POST Request: Create a new task. Provide a JSON body with title and description fields. http POST http://localhost:3000/task Content-Type: application/json { "title": "Task Title", "description": "Task Description" }

PUT Request: Update an existing task. Replace :id with the ID of the task you want to update. Provide a JSON body with the fields you want to update. http PUT http://localhost:3000/tasks/:id Content-Type: application/json { "title": "Updated Task Title" }

GET Request: Retrieve a specific task by replacing :id with the ID of the task. http GET http://localhost:3000/tasks/:id

Closing the Application To stop the Node.js application, press Ctrl+C in the terminal where the app is running.

Notes Ensure that your MongoDB instance is running and accessible before starting the application.

Be cautious with sensitive information like MongoDB URIs and passwords. Do not share them in public repositories or with unauthorized individuals.

You can customize and expand the API functionality to suit your project's requirements by modifying the server.js file and the taskModel.js file in the models directory.

Feel free to adapt this README.md to your specific project's needs, and don't forget to update it with any additional information or usage instructions as your project evolves.

Releases

No releases published

Packages

No packages published