json-server--mock數(shù)據(jù)制造

STEP1==:配置或下載json-server
本地安裝
首先你本地要裝有nodejs。之后直接安裝json-server: sudo npm install -g json-server
git地址:https://github.com/typicode/json-server
STEP2==:安裝node依賴
運行npm i,安裝package.json下的依賴
STEP3==:在json-server目錄中db.json文件中造數(shù)據(jù)
在db.json文件下創(chuàng)建自己需要的數(shù)據(jù),如:
{
"reData":{
"returnCode": "0000",
"returnMsg": "operate successfully",
"returnData": {
"name": "jack"
}
}
}
注:為了保證統(tǒng)一,造數(shù)據(jù)結(jié)構(gòu)及字段要和后臺一致。
STEP4==:在routes.json中配置路由
{
"/linkTest":"/reData"
}
STEP5==:設(shè)置代理轉(zhuǎn)發(fā)
先在項目config/index.js文件中,配置代理轉(zhuǎn)發(fā);
var path = require('path')
module.exports = {
stg: {
env: require('./dev.env'),
port: 8087,
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
'/linkTest': {
target: 'http://localhost:3000/', //這是json-server默認起的服務(wù)
changeOrigin: true,
pathRewrite: {
'^/linkTest': '/linkTest'
}
}
}
}
}
STEP6==:設(shè)置請求地址
$.ajax({
//url: App.Api.prefix + '/member/getMemberView', // 正常請求
url: 'https://www.baidu.com/linkTest', // 可以
//url: 'http://localhost:8087/linkTest', // 也可以
type: 'get',
注:1.請求地址中一定要有配置路由中設(shè)置的 '/linkTest',這個名稱不要與正常請求相同,而前面的域名部分可以隨意,如https://www.baidu.com/,只是后面有且只有一個分類目錄“/linkTest”,請求為http或https;
2.請求類型必須為"get",但請求正常接口,要將type修改哦
STEP7==:啟動項目服務(wù)
啟動后,終端中會提示:
[HPM] Proxy created: /linkTest -> http://localhost:3000/
[HPM] Proxy rewrite rule created: "^/linkTest" ~> "/linkTest"
...
STEP8==:啟動json-server服務(wù)
Resources
http://localhost:3000/reData
...
注意事項==:
1.要開啟兩個服務(wù),即你項目的服務(wù)(http://localhost:8087/)和json-server的服務(wù)(http://localhost:3000/
2.若是請求類型為“post”,請求后,db.json中的數(shù)據(jù)會變成如下:
"reData": {
"ticket": "S8f89496e5cad46c19f9639586323c296001441",
"memberId": "1021885925"
},

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容