GithubHelp home page GithubHelp logo

express-mongo-project's Introduction

README for Node.js Express and Mongoose API

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.

Requirements

  • Node.js
  • MongoDB
  • Express
  • Mongoose
  • CORS

Setup

  1. Install Node.js: Ensure Node.js is installed on your system. You can download it from Node.js official website.

  2. Install MongoDB: Follow the instructions on MongoDB's official documentation to install MongoDB on your system.

  3. Clone/Download the Project: Clone or download the project to your local machine.

  4. 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
  5. Start MongoDB: Ensure your MongoDB server is running.

  6. Start the Application: Run the following command in the terminal:

    node <your-script-name>.js

API Endpoints

General Structure

  • All responses are in JSON format.
  • Error handling is included for database connection issues and invalid requests.

Endpoints

  1. GET /: Returns a simple "Hello world!" message.
  2. GET /products: Fetches all products.
  3. GET /products/:id: Fetches a single product by its ID.
  4. PUT /products/:id: Updates a product by its ID with provided data.
  5. DELETE /products/:id: Deletes a product by its ID.
  6. GET /products/:id/:field: Fetches a specific field of a product by its ID.
  7. PATCH /products/:id/:field: Updates a specific field of a product by its ID.
  8. GET /products/page/:skip/:limit: Fetches products with pagination.

Example Usage

1. GET / (Root Endpoint)

Fetches a simple greeting message.

    fetch('/').then(response => response.text())
        .then(data => console.log(data)); // Logs "Hello world!"

2. GET /products (Fetch All Products)

Fetches all products from the database.

    fetch('/products').then(response => response.json()).
        then(products => console.log(products)); // Logs all products

3. GET /products/:id (Fetch Product by ID)

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

4. PUT /products/:id (Update Product by ID)

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

5. DELETE /products/:id (Delete Product by ID)

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

6. GET /products/:id/:field (Fetch Specific Field of a Product)

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

7. PATCH /products/:id/:field (Update Specific Field of a Product)

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

8. GET /products/page/:skip/:limit (Paginated Fetch of Products)

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

Notes

  • 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.

License

This project is open-source and available under the MIT License.

express-mongo-project's People

Contributors

ashishbarvaliya avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.