不少事情,標準和最佳實踐是一種平衡node
看那本rest api會被玩死,並不實用mysql
rest自己是好東西,其實我只要取其精華就行了git
不必徹底按照它作,理解狀態變化就行了github
本身去實現一下,https://developer.github.com/v3/sql
寫起來仍是挺那啥的,舉個例子mongodb
Current Version By default, all requests receive the v3 version of the API. We encourage you to explicitly request this version via the Accept header. Accept: application/vnd.github.v3+json
核心是We encourage you to explicitly request this version via the Accept header.
,這是很是標準的rest api寫法express
但是有多人理解Content-Type(Mime-Type)是啥呢?尤爲還要自定義,若是你不理解怎麼能用好呢?json
再舉個例子:link-header
api
https://developer.github.com/v3/#link-headerrestful
The pagination info is included in the Link header. It is important to follow these Link header values instead of constructing your own URLs. In some instances, such as in the Commits API, pagination is based on SHA1 and not on page number.
Link: <https://api.github.com/user/repos?page=3&per_page=100>; rel="next", <https://api.github.com/user/repos?page=50&per_page=100>; rel="last"
分頁信息裏包含link-header
,是很是好的表達了狀態,從上面的例子,咱們能夠很輕鬆的理解下一頁和最後一頁的
但問題是
標準和最佳實踐是一種平衡,一種取捨,有些事情是作了錦上添花,有的是必須的,那麼咱們能作的就是,作好必須得,若是能錦上添花更好
現實中的開發仍是按照本身項目狀況來取捨的。
舉個土,但實用的例子:微博的接口
它是比較適合國人思惟的
http://open.weibo.com/wiki/2/statuses/public_timeline
好比我寫的res.api就是一個約定
res.api is an express middleware for render json api , it convention over api format like this :
{ data: { }, status: { code : x, msg : 'some message' } }
若是api都這樣約定,開發速度會很是快,並且簡單
mount-routes裏使用的是mount-routes。
mount-routes裏的作法是自動加載某個目錄,好比app/routes下的全部目錄,子目錄做爲路由
api的設計能夠這樣玩
app/routes/api/v3/users
它能夠自動映射到url裏,對於版本控制仍是比較友好的
先給幾個簡單的
其實還有一個個人解決方案,使用個人moajs
首先建立moajs插件
nmm init
使用腳手架,建立模型和基本的rest接口
moag location name:string order_num:string is_full:string is_available:string warehouse_id:string shelf_id:string
這裏會自動建立api目錄和對應的文件的
測試這些接口,執行moas便可
moas
這樣就能夠測試接口了。
目前moajs的狀態實現了足夠的features,但穩定性和文檔等不足,各位能夠先拿moa-api玩玩,其餘高級特性待穩定後放出
全文完
歡迎關注個人公衆號【node全棧】