-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Week4 hw #5
Conversation
request( | ||
`${url}?_limit=20`, | ||
(error, response, body) => { | ||
const json = JSON.parse(body); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
轉換 JSON 的部分,可以使用 try..catch
包住,理由可以看 week4 作業檢討 :)
`${url}/${bookId}`, | ||
(error, response, body) => { | ||
const json = JSON.parse(body); | ||
// console.log(json); // { name: '哈利波特-神秘的魔法石', id: 11 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
這句應該是忘記刪
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
這是過程搭配 console 輸出的記錄,留著提醒自己 :)
|
||
const request = require('request'); | ||
|
||
const url = 'https://restcountries.eu/rest/v2'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
以這題來說,可以接 https://restcountries.eu/rest/v2/name
也 OK
}; | ||
|
||
function callback(error, response, body) { | ||
const data = JSON.parse(body); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建議這裡也要做 statusCode 的判斷 :)
API 的全名是 Application Programming Interface,應用程式介面,這坨單字裡最重要的就是 "interface" (介面),例如 CLI、GUI 裡也都有的 interface,API 是一種拿來和其他應用程式「溝通」的媒介、管道。CLI 和 GUI 是和電腦溝通的媒介,溝通的目的是為了控制電腦;而 API 是和其他應用程式溝通的媒介,溝通的目的則是為了交換資料。 | ||
但是其它的應用程式不會讓我們無限制的、我們要看什麼資料就給我們什麼,更不會讓我們自由進出他們的資料庫!這個時候 API 的重要性就出現了:API 規定了可以用什麼方式、存取什麼樣的資料,一切的規定都寫在那個 API 的文件。 | ||
有應用程式的地方就有 API,以網頁開發來說使用到的就是 Web API。Web API 通常指的是基於 HTTP 協定下運作的 API,常見的應用情境有會員登入系統、廣告、社群互動嵌入、資料嵌入等。 | ||
開發者最常使用 Web API 進行的四種行為包括 Create (新增)、Read (讀取)、Update (更新) 和 Delete (刪除),也就是 CRUD。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Web API 的確不容易定義,因為同學的回答我這邊也查了一下資料,大致上沒錯。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
這裡在下定義時的確找完資料有偷懶看 Miahsu 和 yakim-shu 的 blog 參考怎麼寫比較容易懂,所以這裡要來補充一些看完助教回饋查的資料。
看了 O'Reilly Designing Web APIs Chapter 2 講到 Web API paradigms 大致分成 Request-Response 和 Event-driven 兩大類。Request-Response APIs 包含
- REST
- RPC (e.g. SOAP)
- GraphQL
這三個概念,這類 API 透過 HTTP 協定運作的 web server 交換資料。Event-driven APIs 則包含
- WebHooks
- WebSockets
- HTTP Streaming
這三個機制,WebHooks 是當某件事發生的時候,系統會自動告訴開發者發生了這件事,事件由開發者定義;WebSockets 本身和 HTTP 一樣是一個基於 TCP 的應用層傳輸協定,browser 和 server 只需要經過一次握手就可以開始雙向交換資訊;HTTP Streaming 則是 request–response 的變形,server 會透過 client 打開的通道持續的推送新資料,Facebook 和 Twitter 都是使用這個機制持續向使用者推播、更新 timeline。
最後總結,嚴格來說「基於 HTTP 協定下運作的 API」指的是 Request-Response APIs,Event-driven APIs 除了 WebSockets,另外兩個廣義上來講也是基於 HTTP。
歡迎助教(或是任何看到的人)補充或指正~~
作業做得還不錯 :) ,需要改進的部分都在 week4 作業檢討中了,恭喜你完成了第四週的課程,week5 可以選擇放鬆一下或複習之前所學,也推薦做小挑戰,加油! |
- 504 代表連線逾時,client-side 無法取得 server 的 response,因此顯示服務沒有回應。 | ||
- 如果不趕快修復 504 Gateway Timeout 可能會對網站 SEO 造成負面影響,因為 504 會造成 Google 爬蟲無法正常擷取,影響網站擷取頻率,進而影響網站排名。 | ||
- ref.: MDN, [WordPress 出現 504 Gateway Timeout Error 解決方法?](https://techmoon.xyz/504-gateway-timeout-error-fix-wordpress/) | ||
- 418 I’m a teapot. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
完成 Week 4 hw~