An open API service indexing awesome lists of open source software.

https://github.com/valentinocarmonas/api-crud


https://github.com/valentinocarmonas/api-crud

Last synced: 5 months ago
JSON representation

Awesome Lists containing this project

README

          

# API CRUD

![Build Status](https://github.com/ValentinoCarmonaS/API-CRUD/actions/workflows/ci.yml/badge.svg)
[![Codecov](https://codecov.io/gh/ValentinoCarmonaS/API-CRUD/branch/main/graph/badge.svg)](https://codecov.io/gh/ValentinoCarmonaS/API-CRUD)

A **RESTful API** built with **Node.js**, **Express**, and **MongoDB**,
implementing the **MVC** pattern. It supports **CRUD** operations for user
management and includes **JWT authentication** for secure access. Designed for
scalability, thoroughly tested, and easy to deploy.

## ๐Ÿš€ Features

- **CRUD Operations**: Create, read, update, and delete users.
- **Authentication**: Secure endpoints with JWT-based authentication.
- **MongoDB**: NoSQL database integration with MongoDB Atlas.
- **Testing**: Comprehensive tests with Jest and Supertest (93.75% branch
coverage).
- **Docker**: Containerized setup for easy deployment.

## ๐Ÿ“‹ Prerequisites

- **Node.js** v16+
- **Docker** and **Docker Compose** (optional, for containerized setup)
- **MongoDB Atlas** account or local MongoDB instance

## ๐Ÿ› ๏ธ Installation

1. Clone the repository:

```bash
git clone https://github.com/ValentinoCarmonaS/API-CRUD.git
cd API-CRUD
```

2. Install dependencies:

```bash
npm install
```

3. Create a `.env` file in the root directory with:
```bash
PORT=3000
MONGODB_URI=mongodb+srv://:@cluster0.mongodb.net/api-crud?retryWrites=true&w=majority
JWT_SECRET=your_jwt_secret
```

## โ–ถ๏ธ Running the API

- **Locally**:

```bash
npm start
```

The API will be available at `http://localhost:3000/api`.

- **With Docker**:
```bash
make build
make up
```
Stop containers with:
```bash
make down
```

## ๐Ÿงช Testing

Run tests with Docker:

```bash
make test
```

Or run test with npm:

```bash
npm test
```

- Tests cover authentication, user CRUD, and global middleware logic.
- Coverage: 93.75% branches, 98.64% statements.
- View detailed reports in `coverage/lcov-report/index.html`.

## ๐Ÿ“š API Endpoints

- **Auth**:
- `POST /api/auth/register`: Register a new user.
- `POST /api/auth/login`: Authenticate and receive a JWT.
- **Users** (JWT required):
- `GET /api/users`: List all users.
- `GET /api/users/:id`: Get a user by ID.
- `POST /api/users`: Create a user.
- `PUT /api/users/:id`: Update a user.
- `DELETE /api/users/:id`: Delete a user.

For detailed endpoint documentation, refer to the
[API specification](docs/api-spec.md).

## ๐Ÿ—‚๏ธ Project Structure

```
API-CRUD/
โ”œโ”€โ”€ src/
โ”‚ โ”œโ”€โ”€ config/ # Database and environment setup
โ”‚ โ”œโ”€โ”€ controllers/ # Business logic for endpoints
โ”‚ โ”œโ”€โ”€ middlewares/ # Authentication and validation
โ”‚ โ”œโ”€โ”€ models/ # MongoDB schemas
โ”‚ โ”œโ”€โ”€ routes/ # API endpoint definitions
โ”‚ โ”œโ”€โ”€ tests/ # Jest and Supertest tests
โ”‚ โ””โ”€โ”€ app.js # Express app setup
โ”œโ”€โ”€ .env # Environment variables
โ”œโ”€โ”€ Dockerfile # Docker configuration
โ”œโ”€โ”€ docker-compose.yml # Docker Compose setup
โ”œโ”€โ”€ Makefile # Automation scripts
โ””โ”€โ”€ package.json # Dependencies and scripts
```

## ๐Ÿ“œ License

[MIT](LICENSE)