Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/quiddlee/node-crud-api

๐Ÿ’š Node.js CRUD API | The Rolling Scopes School task
https://github.com/quiddlee/node-crud-api

crud-api mvc nodejs rest-api rs-school

Last synced: about 2 months ago
JSON representation

๐Ÿ’š Node.js CRUD API | The Rolling Scopes School task

Awesome Lists containing this project

README

        

# ๐Ÿ’š Node CRUD API

*๐Ÿฆฅ RS-School task.*

# Getting Started ๐Ÿš€
To run the project locally, you would have to download zip file with the repository or clone it to your computer. โœจ

## Setup and Running โš ๏ธ

What things do you need to do in order to run our project locally? ๐Ÿค”

* Use node 20 LTS โšก
* Installed [.git](https://git-scm.com/) on your computer. โœŒ๏ธ
* Code Editor of your choice. ๐Ÿ“
* Installed [npm](https://www.npmjs.com/). ๐Ÿ“ฆ

## Installation And Preparation ๐Ÿ”ฎ

First make sure you have all the things listed in the previous section. Then clone our repository to your computer: ๐Ÿ‘Œ

```
git clone https://github.com/Quiddlee/node-crud-api.git
```

or download zip file manually with our repository.

Navigate into project folder and run ๐Ÿ“ฆ:

```
npm install
```

Create ```.env``` file in the root of the project and add all necessary variables ๐Ÿ”ฅ.

You can find ```.env.example``` as an example file in the project root or follow the lines below ๐Ÿบ:

```dotenv
PORT=YOUR_PORT
HOST=YOUR_HOST
```

Finally run a development server: ๐Ÿคฉ
```
npm run start:dev
```
Aaaaand you're done! ๐ŸŽ‰๐Ÿฅณ

## Available Scripts ๐Ÿฅ‘

Here you can find all the scripts that are available in our project. ๐Ÿฆš

Start the app in `base` mode: โœ…

```
npm run start:dev
```

Start the app in `multi` mode: ๐Ÿชญ

```
npm run start:multi
```

Start the app in `prod` mode: ๐Ÿชถ

```
npm run start:prod
```

Lint the app with `eslint`: ๐Ÿฆš

```
npm run lint
```

Lint adn fix the app errors with `eslint`: ๐Ÿจ

```
npm run lint:fix
```

Format the App with **Prettier**: ๐Ÿงน

```
npm run format
```

Format the App with **Prettier** fix: ๐Ÿƒ

```
npm run format:fix
```

Type check the App with **TypeScript**: ๐Ÿฆ

```
npm run type-check
```

Run unit-tests with **Vitest**: ๐Ÿงช

```
npm run test
```
# Working with API ๐Ÿณ
In the project root folder, you can find a Postman collection that will make your life easier while working with the API. ๐Ÿ˜‰

![postman.jpg](./public/postman.jpg)

## API endpoints ๐Ÿฆ‰
The API has the following endpoints:

| Method | Endpoint | Description |
|---------|:--------------:|------------------------------------:|
| GET | /users | Get all the users from the database |
| GET | /users/:id | Get a single user by ID |
| POST | /users | Create a new user in the database |
| PUT | /users/:id | Update a user by ID |
| DELETE | /users/:id | Delete a user by ID |

## Request body ๐Ÿฅ‘

| Endpoint | Body | Example |
|----------|:----------:|----------------------------------------------------------------------------------------------------------------------------:|
| POST | /users | An object with the username age and hobbies ```{"username": "user", "age": 20, "hobbies": ["cooking", "sport"]}``` |
| PUT | /users/:id | An object with the updated username age and hobbies ```{"username": "updated user", "age": 30, "hobbies": ["updated hobbie"]}``` |

## Response format ๐Ÿ‡

| Field | Type | Description |
|---------|:-------------------:|-----------------------------------------------------------------------------:|
| status | "success" or "fail" | Indicates whether the request was successful or not |
| message | string | in case of failed result the response will contain message why it is failed |
| data | Object or Array | The data returned by the request |

## Response examples ๐Ÿ‹

**GET /users**

```json
{
"status": "success",
"data": {
"users": [
{
"username": "user",
"age": 20,
"hobbies": ["cooking", "sport", "programming"]
},
{
"username": "user2",
"age": 21,
"hobbies": ["sport", "programming"]
},
{
"username": "user3",
"age": 22,
"hobbies": ["hobbie"]
}
]
}
}
```

**GET /users/:id Error case**

```json
{
"status": "fail",
"message": "๐Ÿ˜ฏ User not found"
}
```