The following is an example of using ExpressJs, Sequelize(ORM) using PostgreSQL as the database, then you can take a reference if you are just starting to migrate to express, this template includes middleware for authorization and authentication. In this project I also include an example of integrated testing, using JEST and Supertest, which you can learn about that.
- Signin
- Signup
- Create task
- Update task
- Authorization when delete and updating task
- Authentication using jwt (JSON Web Token)
first install all modules
npm install
Next we call db:create
to create a database
npx sequelize-cli db:create
Then we execute migration to create user migration, and task
npx sequelize-cli db:migrate
then we run our seeder which is in ./app/seeders
, so that there is our initial data
npx sequelize-cli db:seed:all
and we run the server so that it can be tested on postman or using insomnia
npm run serve
First, we create a database for testing
npm run db:create:test
then we can do testing
npm test
Here are the details of each endpoint
All API must use this authentication except sign in and sign up
Request :
- Header :
- x-api-key : "your secret api key"
Request :
- Method : POST
- Endpoint :
/api/v1/signup
- Header :
- Content-Type: application/json
- Accept: application/json
- Body :
{
"name": "string",
"username": "string",
"email": "string",
"password": "string"
}
- Reponse :
{
"msg": "string",
"data": {
"id": "int",
"name": "string"
}
}
Request :
- Method : POST
- Endpoint :
/api/v1/signin
- Header :
- Content-Type: application/json
- Accept: application/json
- Body :
{
"email": "string",
"password": "string"
}
- Reponse :
{
{
"msg": "string",
"key": "string"
}
}
Request :
- Method : GET
- Endpoint :
/api/v1/users
- Header :
- Accept: application/json
- Response :
{
"msg": "Success retrieve data users",
"data": [object]
}
Request :
- Method : GET
- Endpoint :
/api/v1/users/id
- Header :
- Accept: application/json
- Response :
{
"msg": "Success retrieve detail user",
"data": {
"id": "int",
"name": "string",
"username": "string",
"email": "string",
"password": "string",
"createdAt": "date",
"updatedAt": "date",
"Tasks": [
{
"id": "int",
"title": "string",
"description": "string",
"userId": "int",
"createdAt": "date",
"updatedAt": "date"
},
]
}
}
Request :
- Method : PUT
- Endpoint :
/api/v1/users/{id}
- Header :
- Content-Type: application/json
- Accept: application/json
- Body :
{
"name": "string",
"username": "string",
"email": "string",
"password": "string"
}
- Reponse :
{
"msg" : "string",
}
Request :
- Method : GET
- Endpoint :
/api/v1/tasks
- Header :
- Accept: application/json
- Response :
{
"msg" : "string",
"data" : [{
"id": "int",
"title": "string",
"description": "string",
"userId": "int",
"createdAt": "date",
"updatedAt": "date",
"User": {
"id": "int",
"name": "string",
"username": "string",
"email": "string",
"password": "string",
"createdAt": "date",
"updatedAt": "date"
}
}]
}
Request :
- Method : POST
- Endpoint :
/api/v1/tasks
- Header :
- Content-Type: application/json
- Accept: application/json
- Body :
{
"title": "string",
"description": "string",
"userId": "int"
}
- Reponse :
{
"msg" : "string",
"data" : {
"id": "int",
"title": "string",
"description": "string",
"userId": "int",
"createdAt": "date",
"updatedAt": "date"
}
}
Request :
- Method : PUT
- Endpoint :
/api/v1/tasks/{id}
- Header :
- Content-Type: application/json
- Accept: application/json
- Body :
{
"title": "string",
"description": "string",
"userId": "int"
}
- Reponse :
{
"msg" : "string",
}
Request :
-
Method : DELETE
-
Endpoint :
/api/v1/tasks/{id}
-
Header :
- Accept: application/json
-
Reponse :
{
"msg" : "string"
}
MIT