[TOC]
搭建restful API很簡單,引入**cors中間件**即可,不需要設置請求頭為**Access-Control-Allow-Origin**,這個中間件會自動幫我們設置。
# 演示代碼
```javascript
const Koa = require('koa');
const logger = require('koa-logger');
const Router = require('koa-router');
const cors = require('@koa/cors');
const app = new Koa();
//記錄日志
app.use(logger());
//支持跨域請求
app.use(cors());
// 主頁
let routerHome = new Router();
routerHome.get('/', async (ctx, next) => {
ctx.body = '歡迎歡迎!';
})
// restful api
let routerRest = new Router();
routerRest.get('/resource', async (ctx, next) => {
ctx.body = {
errno: 0,
errmsg: 'GET API執行成功',
data: '返回的數據'
};
}).post('/resource', async (ctx, next) => {
ctx.body = {
errno: 0,
errmsg: 'POST API執行成功',
data: '返回的數據'
};
})
// 裝載所有路由
let router = new Router();
router.use('/', routerHome.routes(), routerHome.allowedMethods());
router.use('/restful', routerRest.routes(), routerRest.allowedMethods());
app.use(router.routes(), router.allowedMethods());
//監聽3000端口
app.listen(3000, () => {
console.log('server is running at http://localhost:3000')
});
```
# 測試
> 使用curl命令測試RESTful API
## GET請求
```
$curl http://localhost:3000/restful/resource
```
## POST請求
```
$curl http://localhost:3000/restful/resource -X POST -H "Content-Type:application/json" -d '{"a":"abc","b":101}'
```
> 可以在瀏覽器中裝一個RESTful插件發送請求測試

# 使用curl命令測試RESTful API
Representational State Transfer,簡稱REST 是一種全新的軟件架構風格、設計風格(特別在http web服務領域)。
curl是利用URL語法在命令行方式下工作的開源文件傳輸工具。它被廣泛應用在Unix、多種Linux發行版中。
**curl基本語法**
```
curl [option] [url]
```
可選的option有很多,可通過 curl --help 查看,重點介紹下幾個常用的選項。?
-X? ? 或者 --request? ? 指定請求方式,如GET 、POST 、PUT 、DELETE 、HEAD 等七種方式
-i? ?或者? --include? ? 顯示服務器response 響應頭部信息
-v? 或者? --verbose? ? 顯示詳細(冗長)信息
-H? 或者? --header? 指定http 請求頭 ,如? -H? "Content-Type:application/json"?
-d? 或者? --data? 指定請求體body參數 , 如有多個參數可以用&隔開或者使用多個-d 選項。 如? -d "a=abc&b=110&c=true" (指定三個參數)或? -d a=abc -d b=110 -d c=true 亦可。
-F? 或者? --form? 指定multipart Form 請求體,如文件域參數,或者普通表單域參數。
-u 或者? --user? 指定用戶名:密碼?
-C? 或者? --continue-at? offset? 用于斷點續傳。
-c? ?或者 --cookie-jar? ?請求返回回寫cookie到指定文件中
-D? 或者? --dump-header? 請求返回回寫response header信息到指定文件中
-b? 或者? --cookie? 請求時攜帶上cookie,指定本地cookie所在文件?
-x? 或者? --proxy? ?指定 http代理服務器ip:port?
-O? 請求Url 并保存到本地
-o? 請求Url并保存到指定文件
1、直接請求
? curl? ? http://www.tingcream.com? ? #默認? 使用get 請求方式,content-type為form-unlencoded??
? curl? ?-i? ? http://www.tingcream.com? ?# -i 顯示response head信息
2 、下載url資源(文件、圖片)到本地
下載圖片到本地,圖片名稱為服務器默認確定的名稱
```
$?curl -O? ?http://www.tingcream.com/imgSev/tcblog/image/20180320/20180320235916.522_127.png? ?
```
下載圖片到本地并指定文件名稱
```
$curl? -o 127.png http://www.tingcream.com/imgSev/tcblog/image/20180320/20180320235916.522_127.png?
```
3 、請求時指定請求方式
```
$curl? ? www.tingcream.com? -X POST?
```
> -X POST或者 -X post?,-X PUT 或者 -X put? ? # 使用post、put方式請求服務
4 、使用查詢字符串傳參(get)
```
? ?curl? http://xxx.com?username=zhangsan&pwd=123??
```
5 、使用post body傳參數
```
? ?curl? -v http://xxxx.com? -X POST -d username=zhangsan -d password=123456?
```
或者
```
? ?curl -v? http://xxxx.com? -X POST -d? 'username=zhangsan&password=123456'??
```
6 、使用multipart Form 傳參(文件上傳)
? ?curl? ?http://xxx.com? ?-X POST? -F "myFile=@/usr/local/aaa.txt" (文件參數)? -F "username=zhangsan" (普通文本參數)
7、cookie的回寫與使用
? ? curl -c? ~/cookie.txt? ?http://xxx.com? ?#請求回寫cookie到本地文件
? ? curl -b? ~/cookie.txt? http://xxx.com? ?#再次請求,攜帶上cookie (sesisonId)
8、使用http 代理服務器訪問url
? ?curl? ?-x? proxyIP:proxyPort? http://xxx.com??
9 、使用-C? 進行斷點續傳?
11 、restFull API?使用Post JSON
```
$curl? http://xxx.com? ?-X POST -H "Content-Type:application/json"? -d? '{"a":"abc","b":101}'?
```
- 內容介紹
- EcmaScript基礎
- 快速入門
- 常量與變量
- 字符串
- 函數的基本概念
- 條件判斷
- 數組
- 循環
- while循環
- for循環
- 函數基礎
- 對象
- 對象的方法
- 函數
- 變量作用域
- 箭頭函數
- 閉包
- 高階函數
- map/reduce
- filter
- sort
- Promise
- 基本對象
- Arguments 對象
- 剩余參數
- Map和Set
- Json基礎
- RegExp
- Date
- async
- callback
- promise基礎
- promise-api
- promise鏈
- async-await
- 項目實踐
- 標簽系統
- 遠程API請求
- 面向對象編程
- 創建對象
- 原型繼承
- 項目實踐
- Classes
- 構造函數
- extends
- static
- 項目實踐
- 模塊
- import
- export
- 項目實踐
- 第三方擴展庫
- immutable
- Vue快速入門
- 理解MVVM
- Vue中的MVVM模型
- Webpack+Vue快速入門
- 模板語法
- 計算屬性和偵聽器
- Class 與 Style 綁定
- 條件渲染
- 列表渲染
- 事件處理
- 表單輸入綁定
- 組件基礎
- 組件注冊
- Prop
- 自定義事件
- 插槽
- 混入
- 過濾器
- 項目實踐
- 標簽編輯
- 移動客戶端開發
- uni-app基礎
- 快速入門程序
- 單頁程序
- 底部Tab導航
- Vue語法基礎
- 模版語法
- 計算屬性與偵聽器
- Class與Style綁定
- 樣式與布局
- Box模型
- Flex布局
- 內置指令
- 基本指令
- v-model與表單
- 條件渲染指令
- 列表渲染指令v-for
- 事件與自定義屬性
- 生命周期
- 項目實踐
- 學生實驗
- 貝店商品列表
- 加載更多數據
- 詳情頁面
- 自定義組件
- 內置組件
- 表單組件
- 技術專題
- 狀態管理vuex
- Flyio
- Mockjs
- SCSS
- 條件編譯
- 常用功能實現
- 上拉加載更多數據
- 數據加載綜合案例
- Teaset UI組件庫
- Teaset設計
- Teaset使用基礎
- ts-tag
- ts-badge
- ts-button
- ta-banner
- ts-list
- ts-icon
- ts-load-more
- ts-segmented-control
- 代碼模版
- 項目實踐
- 標簽組件
- 失物招領客戶端原型
- 發布頁面
- 檢索頁面
- 詳情頁面
- 服務端開發技術
- 服務端開發環境配置
- Koajs快速入門
- 快速入門
- 常用Koa中間件介紹
- 文件上傳
- RestfulApi
- 一個復雜的RESTful例子
- 使用Mockjs生成模擬數據
- Thinkjs快速入門
- MVC模式
- Thinkjs介紹
- 快速入門
- RESTful服務
- RBAC案例
- 關聯模型
- 應用開發框架
- 服務端開發
- PC端管理界面開發
- 移動端開發
- 項目實踐
- 失物招領項目
- 移動客戶端UI設計
- 服務端設計
- 數據庫設計
- Event(事件)
- 客戶端設計
- 事件列表頁面
- 發布頁面
- 事件詳情頁面
- API設計
- image
- event
- 微信公眾號開發
- ui設計規范