Giter Site home page Giter Site logo

prem790 / express-mongodb-crud-api Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 3.13 MB

This project is a straightforward CRUD API using Express.js and MongoDB. It offers endpoints for creating, reading, updating, and deleting data in MongoDB. Perfect for those learning backend development with Node.js, Express, and MongoDB, it provides a solid foundation for more advanced applications.

JavaScript 100.00%
crud-api expressjs mongodb nodejs restful-api

express-mongodb-crud-api's Introduction

Express MongoDB CRUD API

Overview

This project is a basic CRUD (Create, Read, Update, Delete) API built using Express.js and MongoDB. It allows users to interact with a MongoDB database by performing operations like adding new records, retrieving all records, fetching a specific record by ID, updating records, and deleting records. The API is built with simplicity and ease of understanding in mind, making it a great starting point for learning backend development with Node.js and MongoDB.

Technologies Used

  • Node.js: JavaScript runtime environment used for server-side scripting.
  • Express.js: Minimalist web framework for Node.js, used to build the API.
  • MongoDB: NoSQL database used to store and manage data.
  • Mongoose: ODM (Object Data Modeling) library for MongoDB and Node.js, used to interact with the MongoDB database.
  • body-parser: Middleware to parse incoming request bodies in a middleware before your handlers.

Setup and Installation

Prerequisites

  • Node.js and npm installed on your machine.

  • MongoDB installed or access to a MongoDB Atlas cluster.

  • Create a .env file at the root of your project and add the following line:

    DATABASE_URL=<Your MongoDB connection string>
    

Installation Steps

  1. Clone the Repository

    git clone https://github.com/your-username/express-mongodb-crud-api.git
    cd express-mongodb-crud-api
  2. Install Dependencies

    npm install
  3. Start the Server

    npm start

    The server will start running on http://localhost:3000.

Project Structure

  • index.js: The main entry point of the application. It sets up the Express server, connects to MongoDB, and defines the middleware and routes.
  • routes/routes.js: Defines the API endpoints and handles HTTP requests for creating, reading, updating, and deleting data.
  • models/model.js: Defines the MongoDB schema and model used to interact with the MongoDB database.

API Endpoints

POST /api/post

  • Description: Adds a new document to the MongoDB collection.
  • Request Body:
    {
      "name": "John Doe",
      "age": 30
    }
  • Response: Returns the created document.

GET /api/getAll

  • Description: Retrieves all documents from the MongoDB collection.
  • Response: Returns an array of all documents.

GET /api/getOne/:id

  • Description: Retrieves a single document by its ID.
  • Params:
    • id (String): The ID of the document to retrieve.
  • Response: Returns the document with the specified ID.

PATCH /api/update/:id

  • Description: Updates a document by its ID.
  • Params:
    • id (String): The ID of the document to update.
  • Request Body: JSON object containing the fields to update.
  • Response: Returns the updated document.

DELETE /api/delete/:id

  • Description: Deletes a document by its ID.
  • Params:
    • id (String): The ID of the document to delete.
  • Response: Confirmation message with the name of the deleted document.

Running the Project

  1. Start the Server

    npm start

    This will run the server on http://localhost:3000.

  2. Using the API You can use tools like Postman or cURL to interact with the API endpoints listed above.

Error Handling

The API includes basic error handling to ensure that meaningful error messages are returned when something goes wrong, such as when a resource is not found or a request is malformed.

Contributing

If you'd like to contribute to this project, feel free to fork the repository, create a new branch, and submit a pull request. Contributions, improvements, and bug fixes are always welcome!

Contact

For any questions or feedback, feel free to reach out via [email protected].

express-mongodb-crud-api's People

Contributors

prem790 avatar

Watchers

 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.