Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Node.js CRUD API with MongoDB and Swagger

A minimal, complete reference implementation of a REST CRUD API: Node.js, Express, MongoDB via Mongoose, documented and testable through Swagger UI.

What this is. A deliberately small, readable starting point — the whole API is a handful of files, so the request → route → model → response path is visible end to end. Useful as a scaffold for a new service, or as a reference for wiring Swagger to an Express app. It is not a production template: there's no auth, validation layer, or test suite.

Features

  • Node.js & Express for routing and server handling.
  • MongoDB as the database, using Mongoose to interact with MongoDB collections.
  • Swagger UI for testing and documenting the API endpoints.
  • Full CRUD operations (Create, Read, Update, Delete) for managing user data.

Prerequisites

Before you begin, ensure you have the following installed:

Getting Started

Step 1: Clone the repository

git clone <repository-url>
cd <repository-directory>

Step 2: Install dependencies

Run the following command to install all required Node.js packages:

npm install

Step 3: Start MongoDB

If MongoDB is not installed as a service on your machine, you’ll need to start the MongoDB server manually:

mongod

MongoDB will start, listening on the default port 27017.

Note: If you have MongoDB installed as a service, it will start automatically.

Step 4: Start the Node.js Server

Run the following command to start the Node.js server:

node app.js

Once the server starts, you’ll see the following logs:

MongoDB connected
Server running on http://localhost:3000
Swagger Docs available at http://localhost:3000/api-docs

Step 5: Test API Endpoints with Swagger

This project integrates Swagger for API testing. Swagger automatically generates interactive API documentation where you can test the API directly from your browser. To access the Swagger UI:

Open your browser and navigate to:

http://localhost:3000/api-docs

From here, you can interact with the API:

  • GET all users
  • GET a user by ID
  • POST a new user
  • PUT (update) a user's name
  • DELETE a user by ID

Step 6: API Endpoints

HTTP Method Endpoint Description Example Response
GET /users Retrieve all users [ { "id": "1", "name": "John" } ]
GET /users/{id} Get a user by ID { "id": "1", "name": "John" }
POST /users Create a new user { "id": "2", "name": "Jane" }
PUT /users/{id} Update a user by ID { "id": "2", "name": "Jane Doe" }
DELETE /users/{id} Delete a user by ID User deleted

Examples

  • GET all users: http://localhost:3000/users
  • GET a user by ID: http://localhost:3000/users/1
  • POST a new user: http://localhost:3000/users with JSON body { "name": "Jane" }
  • PUT (update) a user's name: http://localhost:3000/users/1 with JSON body { "name": "Jane Doe" }
  • DELETE a user by ID: http://localhost:3000/users/1

About

Reference implementation of a REST CRUD API — Node.js, Express, MongoDB/Mongoose, documented and testable through Swagger UI.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages