Skip to content

A simple CRUD using Express JS as backend and MongoDB as database

Notifications You must be signed in to change notification settings

mahirsust/nodejs-crud-app-backend

Repository files navigation

Node.js CRUD App Backend

A simple CRUD (Create, Read, Update, Delete) API built using Node.js with Express.js and MongoDB as the database.

Table of Contents

Features

  • RESTful API for managing a resource (e.g. products)
  • Supports CRUD operations:
    • Create a new entry
    • Read entries (single or all)
    • Update an existing entry
    • Delete an entry
  • Uses MongoDB for data storage
  • Express.js for routing and handling HTTP requests
  • Environment configuration using .env file
  • Swagger documentation for API routes

Prerequisites

Make sure you have the following installed on your machine:

  • Node.js (version 14 or higher)
  • npm (Node package manager)
  • MongoDB (Local or remote instance)

Getting Started

1. Clone the Repository

git clone https://github.com/mahirsust/nodejs-crud-app-backend.git
cd nodejs-crud-app-backend

2. Install Dependencies

npm install

3. Environment Configuration

Create a .env file in the root directory of the project. Refer to .env.example for the required variables:

# Environment variables.

# Environment Status
NODE_ENV=your_node_env
# Development port
DEV_PORT=3000
# Production port
PROD_PORT=8080

# DB CONFIG
DB_USERNAME=your_mongodb_username
DB_PASSWORD=your_mongodb_password
DB_NAME=your_mongodb_database_name
DB_CLUSTER_NAME=your_mongodb_cluster_name

Setting NODE_ENV:

  • Use NODE_ENV=development for running the app in development mode.
  • Use NODE_ENV=production for running the app in production mode.

Replace the placeholders with your actual MongoDB credentials and app configurations.

4. Running the App

In Development Mode

npm run dev

The application will run on the port defined in DEV_PORT.

In Production Mode

npm start

The application will run on the port defined in PROD_PORT.

5. Access the API

The base URL for accessing the API is:

http://localhost:[PORT]/

[PORT] is the port number your server is running on (e.g., 3000 or 8080).

You can interact with the API using tools like Postman or curl.

Here are some example endpoints:

  • GET /api/items - Get all items
  • POST /api/items - Create a new item
  • GET /api/items/:id - Get an item by ID
  • PUT /api/items/:id - Update an item by ID
  • DELETE /api/items/:id - Delete an item by ID

Replace items with the resource name (e.g. products).

API Documentation

This project includes a Swagger file (swagger.yaml) for API documentation. You can visualize the documentation using Swagger UI or similar tools. To view the interactive Swagger UI in your browser, use the following URL once the server is running:

http://localhost:[PORT]/api-docs

This URL will give you access to the full API documentation where you can interact with the endpoints directly from the browser.

Folder Structure

├── controllers   # API route logic
├── models        # Database schemas
├── routes        # API routes
├── .env.example  # Environment variable example
├── index.js      # Application entry point
├── swagger.yaml  # Swagger API documentation
└── package.json  # Project metadata and dependencies

Technologies Used

  • Node.js: JavaScript runtime
  • Express.js: Web framework for Node.js
  • MongoDB: NoSQL database
  • Swagger: API documentation

Usage

Once the app is running, you can test the CRUD functionality by sending HTTP requests to the available endpoints. Here are some common scenarios:

  • Create a new item: Send a POST request to /api/items with the item data in the request body.
  • Retrieve all items: Send a GET request to /api/items to get a list of all items in the collection.
  • Retrieve a single item: Send a GET request to /api/items/:id where :id is the ID of the item.
  • Update an item: Send a PUT request to /api/items/:id with the updated data.
  • Delete an item: Send a DELETE request to /api/items/:id.

You can use a tool like Postman, curl, or even a frontend application to interact with the API.

Contributing

Feel free to open issues or submit pull requests. Contributions are welcome!

License

This project is licensed under the MIT License.

About

A simple CRUD using Express JS as backend and MongoDB as database

Topics

Resources

Stars

Watchers

Forks