nodeJs--koa2 REST API
阿新 • • 發佈:2018-12-17
REST API規範
編寫REST API,實際上就是編寫處理HTTP請求的async函式,不過,REST請求和普通的HTTP請求有幾個特殊的地方:
- REST請求仍然是標準的HTTP請求,但是,除了GET請求外,POST、PUT等請求的body是JSON資料格式,請求的
Content-Type
為application/json
; - REST響應返回的結果是JSON資料格式,因此,響應的
Content-Type
也是application/json
。
1、工程結構
2、目錄詳解
package.json:專案描敘
{ "name": "rest-koa", "version": "1.0.0", "description": "rest-koa project", "main": "app.js", "scripts": { "dev": "node --use_strict app.js" }, "keywords": [ "koa", "rest", "api" ], "author": "david pan", "dependencies": { "koa": "2.0.0", "koa-bodyparser": "3.2.0", "koa-router": "7.0.0" } }
(1). controller.js--- 路由集中處理
const fs = require('fs'); // add url-route in /controllers: function addMapping(router, mapping) { for (var url in mapping) { if (url.startsWith('GET ')) { var path = url.substring(4); router.get(path, mapping[url]); console.log(`register URL mapping: GET ${path}`); } else if (url.startsWith('POST ')) { var path = url.substring(5); router.post(path, mapping[url]); console.log(`register URL mapping: POST ${path}`); } else if (url.startsWith('PUT ')) { var path = url.substring(4); router.put(path, mapping[url]); console.log(`register URL mapping: PUT ${path}`); } else if (url.startsWith('DELETE ')) { var path = url.substring(7); router.del(path, mapping[url]); console.log(`register URL mapping: DELETE ${path}`); } else { console.log(`invalid URL: ${url}`); } } } function addControllers(router, dir) { fs.readdirSync(__dirname + '/' + dir).filter((f) => { return f.endsWith('.js'); }).forEach((f) => { console.log(`process controller: ${f}...`); let mapping = require(__dirname + '/' + dir + '/' + f); addMapping(router, mapping); }); } module.exports = function (dir) { let controllers_dir = dir || 'controllers', router = require('koa-router')(); addControllers(router, controllers_dir); return router.routes(); };
(2). rest.js--- 支援rest的中介軟體middleware
a.定義錯誤碼的統一處理
b.統一輸出REST
如果每個非同步函式都編寫下面這樣的程式碼:
// 設定Content-Type:
ctx.response.type = 'application/json';
// 設定Response Body:
ctx.response.body = {
products: products
};
很顯然,這樣的重複程式碼很容易導致錯誤,例如,寫錯了字串'application/json'
,或者漏寫了ctx.response.type = 'application/json'
寫這個中介軟體給ctx
新增一個rest()
方法,直接輸出JSON資料
module.exports = {
APIError: function (code, message) {
this.code = code || 'internal:unknown_error';
this.message = message || '';
},
restify: (pathPrefix) => {
pathPrefix = pathPrefix || '/api/';
return async (ctx, next) => {
if (ctx.request.path.startsWith(pathPrefix)) {
console.log(`Process API ${ctx.request.method} ${ctx.request.url}...`);
ctx.rest = (data) => {
ctx.response.type = 'application/json';
ctx.response.body = data;
}
try {
await next();
} catch (e) {
console.log('Process API error...');
ctx.response.status = 400;
ctx.response.type = 'application/json';
ctx.response.body = {
code: e.code || 'internal:unknown_error',
message: e.message || ''
};
}
} else {
await next();
}
};
}
};
(3). controllers/api.js--- rest api的定義
具體的api定義,這裡可以優化下:不同模組建立資料夾,如products/Api.js, car/api.js ...這樣更清晰
const products = require('../model/products');
const APIError = require('../rest').APIError;
module.exports = {
'GET /api/products': async (ctx, next) => {
ctx.rest({
products: products.getProducts()
});
},
'POST /api/products': async (ctx, next) => {
var p = products.createProduct(ctx.request.body.name, ctx.request.body.manufacturer, parseFloat(ctx.request.body.price));
ctx.rest(p);
},
'DELETE /api/products/:id': async (ctx, next) => {
console.log(`delete product ${ctx.params.id}...`);
var p = products.deleteProduct(ctx.params.id);
if (p) {
ctx.rest(p);
} else {
throw new APIError('400', 'product not found by id.');
}
}
};
(4). model/products.js--- 具體的model邏輯處理
模擬資料庫操作
// store products as database:
var id = 0;
function nextId() {
id++;
return 'p' + id;
}
function Product(name, manufacturer, price) {
this.id = nextId();
this.name = name;
this.manufacturer = manufacturer;
this.price = price;
}
var products = [
new Product('iPhone 7', 'Apple', 6800),
new Product('ThinkPad T440', 'Lenovo', 5999),
new Product('LBP2900', 'Canon', 1099)
];
module.exports = {
getProducts: () => {
return products;
},
getProduct: (id) => {
var i;
for (i = 0; i < products.length; i++) {
if (products[i].id === id) {
return products[i];
}
}
return null;
},
createProduct: (name, manufacturer, price) => {
var p = new Product(name, manufacturer, price);
products.push(p);
return p;
},
deleteProduct: (id) => {
var
index = -1,
i;
for (i = 0; i < products.length; i++) {
if (products[i].id === id) {
index = i;
break;
}
}
if (index >= 0) {
// remove products[index]:
return products.splice(index, 1)[0];
}
return null;
}
};
3、postman除錯
npm run dev
postman測試增、查、刪