Skip to content

gabrield-droid/notes-app-back-end

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

notes-api by dicoding

This is a RESTful API that manages notes. It can add, edit, delete, and show notes.

Limitations:

  • This API only stores the data in an array variable. The data will be lost upon every restart.
  • The current configuration allows CORS for all origins, which is insecure for production environments. You could implement authentication or configure the CORS securely yourself as workarounds.

Data structure:

Data example:

{
   "id": "V1StGXR8_Z5jdHi6",
   "title": "Sejarah JavaScript",
   "createdAt": "2020-12-23T23:00:09.686Z",
   "updatedAt": "2020-12-23T23:00:09.686Z",
   "tags": ["NodeJS", "JavaScript"],
   "body": "JavaScript pertama kali dikembangkan oleh Brendan Eich dari Netscape di bawah nama Mocha, yang nantinya namanya diganti menjadi LiveScript, dan akhirnya menjadi JavaScript. Navigator sebelumnya telah mendukung Java untuk lebih bisa dimanfaatkan para pemrogram yang non-Java.",
}

The id, createdAt, and updatedAt properties are managed by the server. The other properties are input by the client.

Installation

  1. Make sure you have nvm installed. If you don't have, install it using these commands below:
    • curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
      You could skip this step if you would like to install NodeJS in the other way
  2. Install NodeJS v22.10.0 via nvm:
    • nvm install v22.10.0
      nvm use v22.10.0
  3. Install packages required in the project directory:
    • Open a terminal and change the directory to the project directory and type this command below:
      npm install

Run the API

Open a terminal and change the directory to the project directory.

  • Run in production mode

    Type this command to run the API:

    npm run start:prod

    It will start a web server running on host 0.0.0.0, at port 5000 (http://0.0.0.0:5000).

  • Run in development mode

    Type this command to run the API:

    npm run start:dev

    It will start a web server running on host localhost, at port 5000 (http://localhost:5000).

Use Cases and their Request and Response Formats

1. Adding a note

Request:

  • Method: POST
  • Endpoint: /notes
  • Body Request:
    {
       "title": "Judul Catatan",
       "tags": ["Tag 1", "Tag 2"],
       "body": "Konten catatan"
    }

Response:

No. Scenario Status Code Response Body
1 The request succeeds 201 (Created)
                              
{
   "status": "success",
   "message": "Catatan berhasil ditambahkan",
   "data": {
   "noteid": "V09YExygSUYogwWJ"
   }
}
                  
               
2 The request fails 500 (Internal Server Error)
                         
{
   "status": "error",
   "message": "Catatan gagal untuk ditambahkan"
}
                  
               

2. Showing all the notes

Request:

  • Method: GET
  • Endpoint: /notes

Response:

No. Scenario Status Code Response Body
1 There are some notes 200 (OK)

{
   "status": "success",
   "data": {
      "notes": [
         {
            "id":"V1StGXR8_Z5jdHi6B-myT",
            "title":"Catatan 1",
            "createdAt":"2020-12-23T23:00:09.686Z",
            "updatedAt":"2020-12-23T23:00:09.686Z",
            "tags":[
               "Tag 1",
               "Tag 2"
            ],
            "body":"Isi dari catatan 1"
         },
         {
            "id":"V1StGXR8_98apmLk3mm1",
            "title":"Catatan 2",
            "createdAt":"2020-12-23T23:00:09.686Z",
            "updatedAt":"2020-12-23T23:00:09.686Z",
            "tags":[
               "Tag 1",
               "Tag 2"
            ],
            "body":"Isi dari catatan 2"
         }
      ]
   }
}

               
2 There are no notes

{
   "status": "success",
   "data": {
      "notes": []
   }
}

               

3. Showing note's details

Request:

  • Method: GET
  • Endpoint: /notes/{noteId}

Response:

No. Scenario Status Code Response Body
1 The note's id is found 200 (OK)

{
   "status": "success",
   "data": {
      "note": {
         "id":"V1StGXR8_Z5jdHi6B-myT",
         "title":"Catatan 1",
         "createdAt":"2020-12-23T23:00:09.686Z",
         "updatedAt":"2020-12-23T23:00:09.686Z",
         "tags":[
            "Tag 1",
            "Tag 2"
         ],
         "body":"Isi dari catatan 1"
      }
   }
}

               
2 The note's id is not found 404 (Not Found)

{
   "status": "fail",
   "message": "Catatan tidak ditemukan"
}

               

4. Editing a note

Request:

  • Method: PUT
  • Endpoint: /notes/{noteId}
  • Body Request:
    {
       "title":"Judul Catatan Revisi",
       "tags":[
          "Tag 1",
          "Tag 2"
       ],
       "body":"Konten catatan"
    }

Response:

No. Scenario Status Code Response Body
1 The note is successfully updated 200 (OK)

{
   "status": "success",
   "message": "Catatan berhasil diperbaharui"
}

               
2 The note's id is not found 404 (Not found)

{
   "status": "fail",
   "message": "Gagal memperbarui catatan. Id catatan tidak ditemukan"
}

               

5. Deleting a note

Request:

  • Method: DELETE
  • Endpoint: /notes/{noteId}

Response:

No. Scenario Status Code Response Body
1 The note is successfully deleted 200 (OK)

{
   "status": "success",
   "message": "Catatan berhasil dihapus"
}

               
2 The note's id is not found 404 (Not Found)

{
   "status": "fail",
   "message": "Catatan gagal dihapus. Id catatan tidak ditemukan"
}

               

Example: How to use the API using cURL

cURL basic syntax:

curl -X {HTTP METHOD} -H "Content-Type: application/json" -d '{BODY REQUEST}' http://{host}:{port}{ENDPOINT}

The -H "Content-Type: application/json" and -d can be omitted if there is no body request passed on the request.

Example 1: Adding a note (production mode)

curl -X POST -H "Content-Type: application/json" -d '{"title": "Judul Catatan", "tags": ["Tag 1", "Tag 2"], "body": "Konten Catatan"}' http://0.0.0.0:5000/notes

Example 2: Showing all notes (development mode)

curl -X GET http://localhost:5000/notes

About

This project is a simple RESTful API that can add, edit, show, and delete a note.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published