This README document provides instructions on how to set up and use a Node.js Express API connected to a MongoDB database using Mongoose for managing products. The API allows for operations such as fetching, adding, updating, and deleting product records.
- Node.js
- MongoDB
- Express
- Mongoose
- CORS
-
Install Node.js: Ensure Node.js is installed on your system. You can download it from Node.js official website.
-
Install MongoDB: Follow the instructions on MongoDB's official documentation to install MongoDB on your system.
-
Clone/Download the Project: Clone or download the project to your local machine.
-
Install Dependencies: Navigate to your project directory in the terminal and run the following command to install the necessary Node modules:
npm install express mongoose cors
-
Start MongoDB: Ensure your MongoDB server is running.
-
Start the Application: Run the following command in the terminal:
node <your-script-name>.js
- All responses are in JSON format.
- Error handling is included for database connection issues and invalid requests.
- GET
/
: Returns a simple "Hello world!" message. - GET
/products
: Fetches all products. - GET
/products/:id
: Fetches a single product by its ID. - PUT
/products/:id
: Updates a product by its ID with provided data. - DELETE
/products/:id
: Deletes a product by its ID. - GET
/products/:id/:field
: Fetches a specific field of a product by its ID. - PATCH
/products/:id/:field
: Updates a specific field of a product by its ID. - GET
/products/page/:skip/:limit
: Fetches products with pagination.
Fetches a simple greeting message.
fetch('/').then(response => response.text())
.then(data => console.log(data)); // Logs "Hello world!"
Fetches all products from the database.
fetch('/products').then(response => response.json()).
then(products => console.log(products)); // Logs all products
Fetches a single product by its ID.
fetch('/products/1') // Replace 1 with the desired product ID .then(response => response.json()) .then(product => console.log(product)); // Logs the product with ID 1
Updates a product's information by its ID.
fetch('/products/1', { // Replace 1 with the product ID
method: 'PUT',
headers: { 'Content-Type': 'application/json', },
body: JSON.stringify({
name: 'Updated Product Name',
price: 200,
// Other fields to update
}),
}).then(response => response.json())
.then(updatedProduct => console.log(updatedProduct)); // Logs the updated product
Deletes a product by its ID.
fetch('/products/1', { // Replace 1 with the product ID to delete
method: 'DELETE', }) .then(response => response.text())
.then(result => console.log(result)); // Logs confirmation of deletion
Fetches a specific field of a product by its ID.
fetch('/products/1/name') // Replace 1 with product ID, and 'name' with the desired field
.then(response => response.json())
.then(fieldData => console.log(fieldData)); // Logs the specific field data
Updates a specific field of a product by its ID.
fetch('/products/1/price', { // Replace 1 with product ID, and 'price' with the field to update
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ value: 300 }), // Set the new value for the field
}).then(response => response.json())
.then(updatedProduct => console.log(updatedProduct)); // Logs the updated product
Fetches products with pagination, based on skip and limit parameters.
fetch('/products/page/0/10') // Fetches the first 10 products
.then(response => response.json())
.then(products => console.log(products)); // Logs the fetched products
- Ensure MongoDB is running and the connection string in the script matches your MongoDB setup.
- Customize the schema and routes as per your requirements.
- This API does not handle authentication. Implement security measures as needed.
This project is open-source and available under the MIT License.