Node 搭建的 Restful 的風格的 API 中間件沧侥,依賴于 Express 和 Mongoose
前言
npm install "ltz-rest" --save
依賴 mongoose 注冊模型可霎,兩句實現 rest API
//1.引入框架
const rest = require('ltz-rest');
app.use('/', rest);
- 中間件自動生成: restful的約定風格API
URL | HTTP | 功能 |
---|---|---|
/tab | POST | 創(chuàng)建對象 |
/tab | GET | 查詢對象 |
/tab/objectId | GET | 獲取對象 |
/tab/objectId | PUT | 更新對象 |
/tab/objectId | DELETE | 刪除對象 |
一、詳情使用
'use strict';
const mongoose = require('mongoose');
const db = mongoose.connect('mongodb://127.0.0.1:27017/test');
const express = require('express');
const app = express();
//1.引入框架
const rest = require('ltz-rest');
app.use('/', rest);
app.listen(3000);
const Schema = mongoose.Schema;
const student = {
name: String,
age: Number,
sex: String,
xuexiao: {
type: Schema.Types.ObjectId,
ref: 'school'
},
score: {
shuxue: Number,
yuwen: Number
}
};
//注冊 mongoose 的模型
mongoose.model('student', new Schema(student));
const school = {
title: String
};
//注冊 mongoose 的模型
mongoose.model('school', new Schema(school));
由上面可知宴杀,我們有倆注冊模型 student school , 這倆注冊的模型都自動生成API癣朗;本插件依賴 mongoose ,只有注冊到mongoose.model() 才會自動生成 API
下面自動生成 student 5個 API 的使用
1:POST 創(chuàng)建數據
http://localhost:3000/student
body 包含 JSON 數據
{
"age":5,
"xuexiao":"58e8448918493009b7449229",
"name":"小四",
"score":{
"shuxue":89,
"yuwen":77
},
"sex":"woman"
}
返回數據
{
"__v": 0,
"age": 5,
"xuexiao": "58e8448918493009b7449229",
"name": "小四",
"sex": "woman",
"_id": "592a33db1b84a41131db2f56",
"score": {
"shuxue": 89,
"yuwen": 77
}
}
注意 POST旺罢,請求的主體必須是 JSON 格式旷余,而且 HTTP header 的 Content-Type 需要設置為 application/json
2:GET 查詢,所有的特殊性關鍵詞都是“_”開頭
查詢 student 文檔扁达,默認_limit=100
http://localhost:3000/student
查詢 student 文檔正卧,限制一條和跳過一跳:_limit=1&_skip=1
http://localhost:3000/student?_limit=1&_skip=1
查詢 student 文檔,數字大于跪解、小于穗酥、大于等于、小于等于惠遏、不等于的數字查詢砾跃;_gt _lt _gte _lte _ne
http://localhost:3000/student?_where={"score.shuxue":{"_gt":90}} //score.shuxue 大于90的
查詢 student 文檔,字符串根據「 正則表達式 」查詢节吮。 "_regex":"^李"抽高,"_regex":"李","_regex":"李$"透绩,名字開頭帶李,名字有李翘骂,名字結尾是李
http://localhost:3000/student?_where={"name":{"_regex":"^李"}} //名字開頭帶"李"
查詢 student 文檔,連表的顯示帚豪。 也就說xuexiao查詢出來不是id碳竟,查詢出來的是相應的文檔
http://localhost:3000/student?_populate=xuexiao
來一個綜合例子:查詢 student 文檔,score.shuxue 大于90且 name 帶“李”狸臣,xuexiao 連表顯示莹桅,限制一條數據。
http://localhost:3000/student?_where={"score.shuxue":{"_gt":90},"name":{"_regex":"李"}}&_populate=xuexiao&_limit=1
特殊關鍵詞_ | 功能 | 演示 |
---|---|---|
_limit | 限制 | _limit=10烛亦,限制返回10條數據 |
_skip | 跳過 | _skip=10诈泼,跳過10條數據 |
_gt、_lt煤禽、_gte铐达、_lte、_ne | 限制數字:大于檬果、小于瓮孙、大于等于唐断、小于等于、不等于 | _gt=10杭抠,大于10脸甘、_ne=20,不等于20 |
_regex | 字符串根據「 正則表達式 」查詢 | {"_regex":"^李"}祈争,名字開頭帶"李" |
_populate | 連表數據返回 | _populate=xuexiao斤程,顯示 xuexiao 數據角寸,也可以用"."顯示更深層的數據:_populate=xuexiao.local菩混,可以把 xuexiao.local 的數據也展示出來 |
3:GET 根據唯一 id 查詢
查詢 student 文檔,唯一id 592a33bf1b84a41131db2f55 扁藕,如果需要學校數據需要_populate
http://localhost:3000/student/592a33bf1b84a41131db2f55?_populate=xuexiao
4:PUT 根據唯一 id 更新數據
修改數據 PUT沮峡, 年齡修改為18歲。 使用了 _populate 獲取學校信息亿柑。
http://localhost:3000/student/592a33bf1b84a41131db2f55?_populate=xuexiao
body 包含 JSON 數據
{
"age":18
}
返回數據
{
"_id": "592a33bf1b84a41131db2f55",
"age": 18,
"xuexiao": {
"_id": "58ddd5db6216a905ce973de4",
"name": "第一高中",
"__v": 0
},
"name": "小紅2222",
"sex": "woman",
"__v": 0,
"score": {
"shuxue": 99,
"yuwen": 88
}
}
注意 PUT邢疙,請求的主體必須是 JSON 格式,而且 HTTP header 的 Content-Type 需要設置為 application/json
5:DELETE 根據唯一 id 刪除數據
DELETE 刪除文檔 id 為592a33bf1b84a41131db2f55
http://localhost:3000/student/592a33bf1b84a41131db2f55
返回數據
{
"_id": "592a33bf1b84a41131db2f55",
"age": 18,
"xuexiao": "58ddd5db6216a905ce973de4",
"name": "小紅2222",
"sex": "woman",
"__v": 0,
"score": {
"shuxue": 99,
"yuwen": 88
}
}
二望薄、跳過中間件
這個請求中間件不會執(zhí)行疟游,并且調用 next,執(zhí)行下一個中間件痕支。
// ALL:跳過所有的 API 請求
// GET:跳過 GET 請求
// 5個 API:GET,GETID,PUT,POST,DELETE颁虐,可以任意選擇跳過。
rest.skip = {
'student':'ALL',//跳過 student 所有的API
'school': 'GET,GETID'//跳過 school 的 GET 與 GETID,其它的 PUT,POST,DELETE 正常返回結果卧须。
}
GET獲取 school 文檔
http://localhost:3000/school
執(zhí)行失敗另绩,因為上面跳過GET
DELETE school 文檔刪除
http://localhost:3000/school/592a33bf1b84a41131db2f55
執(zhí)行成功,因為上面沒有跳過 DELETE 花嘶。
1.0 實現功能笋籽,配置是否跳過中間件。
0.9 書寫文檔椭员,并且發(fā)布NPM
0.8 實現Population
0.7 查詢端口實現數字大于车海、小于、等于隘击、不等于的數字查詢容劳。
gt lt gte lte ne
0.6 查詢端口實現字符串根據「 正則表達式 」查詢。
_where={name:{"_regex":"^李"}}
0.5 思想總結闸度、代碼設計竭贩、接口設計、寫一些demo莺禁、看文檔留量。
0.4 錯誤堆棧拋向前臺。
0.3 查詢接口實現排序。
0.2 版本已經實現5個接口楼熄,查詢接口僅僅實現了 skip 與 limit忆绰。
0.1 版本搭建框架,調試接口可岂。
- 項目GitHub:https://github.com/liangtongzhuo/ltz-rest
- 個人博客:http://liangtongzhuo.com
- 后期可能增加可視界面错敢。