Restful介面規範

2022-07-09 05:00:24 字數 2072 閱讀 4315

rest,即resources representational state transfer(資源表現層狀態轉化)。

·資源(resources),網路上的乙個實體,每種資源對應乙個特定的uri(統一資源識別符號)

·表現層(representational),資源呈現的形式,比如:json

·狀態轉化(state transfer),http協議裡,四個表示操作方式的基本操作

restful api設計規範

·使用http協議

·url中只有名詞,如add、delete等動詞不應出現

·不同操作由動詞來區分:get,post(新建),put(更新,客戶端需提供改變後的完整資源),patch(更新,客戶端只需提供資源改變的屬性),delete(刪除)

·查詢字串中指定過濾條件

結果· get /collection:返回資源物件的列表(陣列)

· get /collection/id:返回單個資源物件

· post /collection:返回新生成的資源物件

· put /collection/id:返回完整的資源物件

· patch /collection/id:返回更新後的完整資源物件

· delete /collection/id:返回乙個空資源

restfulapi.js

var express = require('express');

var path = require('path');

var bodyparser = require('body-parser');

取當前目錄的絕對路徑

}));

type:'text/!*'

}));*/

console.log('body',req.body);

next();

}); console.error(err);

next();

}); res.send(req.body);

});*/

var users = [, ];

/** * 1. 獲取所有的使用者 curl -v -h 'accept:text/html' http://localhost:8080/users

* 2.

*///1.獲取所有的使用者

var accept = req.headers['accept'];

var accepttype = accept.split(',').map(function (item)

//用優先順序進行排序,取排名最高那個

}).sort(function (a, b) )[0].type;

console.log(accepttype);

if(accepttype == 'text/plain')else if(accepttype == 'text/html'));

}else

});//返回某個使用者資訊 路徑引數

//curl http://localhost:8080/users/1 使用函式過濾器過濾

var id = req.params.id;

var filteredusers = users.filter(function(user));

res.send(filteredusers.length>0?filteredusers[0]:'此使用者不存在');

});//新增加使用者

// -x 指定請求方法 --data 指定請求體的資料

//curl -x post --data "name=zfpx3" http://localhost:8080/users 用body-parser獲取請求體

var addeduser = req.body;

if(addeduser)else);

}});//整體更新全部屬性

restful介面規範

資源 resource 任何東西都是一種資源,例如,一篇部落格文章 表示 representation 資源的一種表現形式,例如,json xml url 每個資源都應該有乙個對應的url uri url是uri的一種,url都有對應的資源,而uri不一定,它強調的是一種資源標識 get 獲得乙個資...

restful介面規範

聯絡兩個物質的媒介,完成資訊互動 web程式中 聯絡前台頁面與後台資料庫的媒介 web介面組成 請求引數 前台按照指定的key提供資料給後台 響應資料 後台與資料庫互動後將資料反饋給前台 狀態碼,狀態資訊,響應資料 web資料請求介面設計規範共10條,可以分為url與響應兩部分 url 響應部分 f...

Restful介面規範

2000年roy fielding博士在其博士 中提出rest representational state transfer 風格的軟體架構模式後,rest就基本上迅速取代了複雜而笨重的soap,成為web api的標準了。restful作為目前最流行的 api 設計規範,一定有著它獨有的魅力 強...