JAZZA132
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 什麼是RESTFUL API REST 是 Representational State Transfer 的縮寫,可譯為「具象狀態傳輸」。 REST 是一種軟體架構風格(並非標準),目的是幫助在世界各地不同軟體、程式在網際網路中能夠互相傳遞訊息。 * GET 讀取資源 * PUT 替換資源 * PATCH 更換資源部分內容 * DELETE 刪除資源 * OPTIONS 回傳該資源所支援的所有 HTTP 請求方法 * CONNECT 將連線請求轉換至 TCP/IP 隧道 * POST 新增資源 ### Router, Routing, Route 在談網路基礎建設時,路由器 (router) 是某種實體設備,負責幫每個資料封包 (packet) 選擇傳輸路徑,扮演類似交通指揮的角色。而「路由器幫資料封包們選擇路徑,引導資料按指定路徑移動」的這個過程,被稱為「routing」。 ### Web 開發裡的路由 轉移到 web app 網路應用程式開發的領域,**路由不再是實體設備,而是指 ```URL 的處理程序```**。這組處理程序會把 HTTP 動詞、URL、和相關的程式碼連接起來。 **我們會在路由系統裡,定義「收到什麼 HTTP request,就執行什麼動作」。** 舉例來說,如果今天收到一支 HTTP request,格式為 GET http://www.example.com/todos ,路由系統會調度出對應的資源,也就是和「瀏覽全部待辦事項」相關的樣板、邏輯、資料等等。 因為有了路由系統,網站對外開放的網址,不需要對應到實際的專案目錄。 ### 如何設計路由? ![](https://i.imgur.com/YvqynbH.png) 對資料的操作,不外乎:新增 (create)、讀取 (read)、更新 (update)、刪除 (delete) 四種動作,通常會簡稱為 CRUD。 而 HTTP method 是有意義的動詞,如 GET、POST、PATCH、DELETE 等,因此 RESTful 的表意設計運用動詞的組合,來表達出對資源的操作方式。 # 路由語義化:RESTful 風格應用 ### 什麼是 RESTful 運用 HTTP 來表達語義的路由設計風格稱為 RESTful API。所謂的 API 是應用程式介面 (application programming interface),網址也是一種應用程式的「介面」,故稱為 API。 RESTful 風格的網址設計強調從路由結構就能看出要對什麼資料、進行什麼操作。舉例來說,如果資料是 todos,那麼 RESTful 風格的 CRUD 路由就會這樣寫: ![](https://i.imgur.com/V60RzKP.png) 使用 RESTful 風格設計的 API,就有了以下幾種優點及限制: 1. 有唯一的URL表示資源位置,統一的 API 接口。(Uniform Interface) 2. 無狀態。(Stateless) RESTful 的狀態,意即 HTTP 的請求狀態,一般 Web 服務中,Server 端和 Client 端交互的資訊,會存在 Server 端的 Session (例如:已登入狀態),在 Client 端再次發送請求的時候,Server 端透過保存在 Server 端的 Session,去執行 request。無狀態的意思,即 Client 端自行保存狀態,在請求 Server 的時候,一併附上給 Server 端,Server 端無保存 Client 端的狀態資訊。 舉例來說,可能在用戶登錄系統時,Server 產生 token 紀錄 user 已登錄系統,然後把 token 還給 Client,在 Client 再次發送請求的時候,把 token 一起發給 Server,這樣 Server 就知道這一個 Client 是已經處於登錄的狀態。 意即所有的資源都可以 URI 定位,而且這個定位與其他資源無關,也不會因為其他資源的變化而變化,資源相互的依賴性降低。 舉一個白話一點的例子:查詢員工工資: 第一步:登錄系統。 第二步:進入查詢工資的頁面。 第三步:搜索該員工。 第四步:點擊姓名查看工資。 這樣的操作流程就是有狀態的,查詢工資的每一個步驟都依賴於前一個步驟,只要前置操作不成功,後續操作就無法執行。如果輸入一個URL就可以直接得到指定員工的工資,這種情況就是無狀態的,因為獲取工資不依賴於其他資源或狀態,這種情況下,員工工資是一個資源,由一個 URL 與之對應可以通過 HTTP 中的 GET 方法得到資源,這就是典型的 RESTful 風格。 RESTful 的優點如下所列: 1. 瀏覽器即可以作為 client 端 2. 可以更高效地利用 cache 來達到更快的回應速度 > 在 server-side,GET 過的資源,如果沒有被變更過,可以利用 cache 機制減少 request。 > 在 client-side,透過 client 端 cache 紀錄 chahe 版本,若向 server 要求資源時發現 server 最新版與 cache 相同,則 client 端直接取用本地資源即可,不需要再做一次查詢 3. 界面與資料分離 4. 節省伺服器的計算資源 5. 可重用! web/android/ios 都可以用, 無痛轉換! **RESTful API 主要由三種元件組成:** ![](https://i.imgur.com/XWpXeZ1.png) 1. Nouns 名詞:定義資源位置的 URL,每個資源在網路上都會有唯一的位置,就如每戶人家都有唯一的地址一樣。 2. Verbs 動詞:對資源要做的動作。 3. Content Types 資源呈現方式:API 資源可以以多種方式表現,最常用的是 JSON,較輕,也較好處理。 一般的 API,可能會是這樣: ``` 獲得資料GET /getData 新增資料POST /createData 刪除資料DELETE /deleteData/1 ``` 若以 RESTful API 風格開發的話: ``` 獲得資料GET /data 新增資料POST /data 刪除資料DELETE /data/1 ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully