Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/hadihammurabi/belajar-expressjs-rest-api

Belajar REST API service dengan ExpressJS
https://github.com/hadihammurabi/belajar-expressjs-rest-api

expressjs nodejs rest-api

Last synced: 7 days ago
JSON representation

Belajar REST API service dengan ExpressJS

Awesome Lists containing this project

README

        

# belajar-expressjs-rest-api
Belajar REST API service dengan ExpressJS

# Table of Contents
* [Installation](#installation)
* [Prerequisites](#prerequisites)
* [Project Setup](#project-setup)
* [Running Project](#running-project)
* [API Spec](#api-spec)
* [User Registration](#user-registration)
* [User Login](#user-login)
* [User Profile](#user-profile)
* [CHANGELOG - How This Project Was Built](./CHANGELOG.md)

# Installation
## Prerequisites
1. NodeJS + NPM latest LTS version.
2. MongoDB 4.4 or newest version.

## Project Setup
1. Clone this project by run following command
```
git clone https://github.com/hadihammurabi/belajar-expressjs-rest-api.git
```

2. Go to **belajar-expressjs-rest-api** directory by run following command
```
cd belajar-expressjs-rest-api
```

3. Install all required dependencies by run following command
```
npm i
```

4. Copy **.env.example** to **.env** file by run following command
```
cp .env.example .env
```

5. Configure app and database options inside **.env** file

# Running Project
Once project ready, you can run using following command.

Production mode:
```
npm start
```

Development mode:
```
npm run dev
```

# API Spec
## User Registration
Create a new user using email, password, name, and gender.

### Request


HTTP Method
POST


Endpoint
/auth/register


Headers

Content-Type: application/json



Body

{

"email": "[email protected]",
"password": "123123",
"nama":"Hadi Hidayat Hammurabi",
"jenis_kelamin":"l"
}


### Response
#### Success


Status
200


Body

{

"_id":"60d5d77b8a4fce15df41b1c1",
"email":"[email protected]",
"nama":"Hadi Hidayat Hammurabi",
"jenis_kelamin":"l",
"__v":0
}


#### Fail


Status
400


Body

{

"error":{
"email": {
"name":"ValidationError",
"message":"Path `email` already used."
}
}
}


## User Login
Login using email and password to get JWT token.

### Request


HTTP Method
POST


Endpoint
/auth/login


Headers

Content-Type: application/json



Body

{

"email": "[email protected]",
"password": "123123"
}


### Response
#### Success


Status
200


Body

{

"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYwZDVkNzdiOGE0ZmNlMTVkZjQxYjFjMSIsImlhdCI6MTYyNDYyNzY3M30.2Sz00zzUNlTtDoUriYNdCPvXJd8uF5iK32y9vB8cJe0",
"type":"Bearer"
}


#### Fail


Status
400


Body

{

"error": {
"credential": {
"name":"AuthError",
"message":"Invalid email or password."
}
}
}


## User Profile
Get user profile that signed in, identified by authorization header that contains JWT token.

### Request


HTTP Method
GET


Endpoint
/auth/profile


Headers

Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYwZDVkNzdiOGE0ZmNlMTVkZjQxYjFjMSIsImlhdCI6MTYyNDYyNzY3M30.2Sz00zzUNlTtDoUriYNdCPvXJd8uF5iK32y9vB8cJe0

### Response
#### Success


Status
200


Body

{

"data": {
"_id":"60d53be41416cf16b979e76d",
"email":"[email protected]",
"jenis_kelamin":"l",
"nama":"Tes User",
"__v":0
}
}


#### Fail


Status
403


Body

{

"message": "Akses tidak diizinkan."
}