关于json-server
JSON-Server 是一个 Node 模块,运行 Express 服务器,你可以指定一个 json 文件作为 api 的数据源。可以对json文件、js脚本生成的json数据、远程json数据进行restful风格的增删改查操作,通过指定一个json文件作为api数据源,可以进行分页、排序、关联查询、范围查询等各种查询操作,是一套完整的模拟 REST API 接口。
github
json-server从入门到精通 写的很全,可以参考这个。
全局安装对应插件
npm install -g json-server
检查是否安装成功
npm list -g json-server
PS D:\work\dorado\aaaaa\json-server-demo> npm list -g json-server
D:\soft\nodejs -> .\ └── json-server@1.0.0-alpha.23
出现这种说明安装成功。
使用 json-server 需要遵守一定的规范:
数据查询要使用 GET。
新增数据要使用 POST。
删除数据要使用 DELETE。
修改数据使用 PUT 和 PATCH。
创建json文件(db.json),名字和文件夹可以随意
{
"list": [
{
"payway": "1",
"capitalallocationbillno": "gjps96796160000.4140002552705546",
"summaryno": "ZB2227681500186",
"exchangecurrencycode": "1",
"exchangeamount": "20029.50",
"currencycode": "2",
"amount": "48202.34",
"operatetime": "1990-07-21",
"customercode": "ICHN0347",
"customername": "曾军",
"paymentstatus": "6",
"operratorname": "黄涛",
"fastflag": "0",
"tabperiod": "2003-03-12",
"memo": "程半进己图资管",
"paymenttime": "2007-01-20",
"in_amount": "2976.90",
"in_currency": "3",
"paymentmode": "1",
"visible": true,
"collpayflag": "0",
"accountno": "355081500180",
"backinfo": "不派难片布水体候上",
"viewPDF": "查看PDF",
"accountname": "路件铁去林气位应且义记断公司",
"inpaymentmethod": "2",
"custaccountno": "9876lakv58",
"custbankno": 14317,
"bankname": "农业银行",
"bankprovince": "河南省",
"bankcity": "六盘水市",
"exchangeflag": "1",
"exchangerate": "1.33",
"bankaccountno": "exsw40929660006.607274295739865"
},
{
"payway": "2",
"capitalallocationbillno": "kyivfuvznr54796160000.4140002552705546",
"summaryno": "ZB2227681500186",
"exchangecurrencycode": "4",
"exchangeamount": "20029.50",
"currencycode": "5",
"amount": "48202.34",
"operatetime": "2002-03-19",
"customercode": "ICHN0347",
"customername": "赖秀兰",
"paymentstatus": "7",
"operratorname": "梁勇",
"fastflag": "1",
"tabperiod": "1987-05-01",
"memo": "难完少儿儿高矿半去下",
"paymenttime": "2019-10-15",
"in_amount": "2976.90",
"in_currency": "6",
"paymentmode": "2",
"visible": false,
"collpayflag": "1",
"accountno": "355081500180",
"backinfo": "厂体很容党不",
"viewPDF": "查看PDF",
"accountname": "地大管真目深许计历好公司",
"inpaymentmethod": "2",
"custaccountno": "9876gdaicb70",
"custbankno": 14317,
"bankname": "招商银行",
"bankprovince": "山东省",
"bankcity": "南阳市",
"exchangeflag": "0",
"exchangerate": "1.33",
"bankaccountno": "rkpdxbk9929660006.607274295739865"
},
{
"payway": "3",
"capitalallocationbillno": "cjuerbxdz41796160000.4140002552705546",
"summaryno": "ZB2227681500186",
"exchangecurrencycode": "7",
"exchangeamount": "20029.50",
"currencycode": "8",
"amount": "48202.34",
"operatetime": "1971-09-22",
"customercode": "ICHN0347",
"customername": "罗刚",
"paymentstatus": "10",
"operratorname": "万军",
"fastflag": "0",
"tabperiod": "2014-05-17",
"memo": "指老六容科称样",
"paymenttime": "2016-06-28",
"in_amount": "2976.90",
"in_currency": "9",
"paymentmode": "1",
"visible": false,
"collpayflag": "0",
"accountno": "355081500180",
"backinfo": "积示动节成边",
"viewPDF": "查看PDF",
"accountname": "过它行周清回件政也位公司",
"inpaymentmethod": "2",
"custaccountno": "9876tnlqj7",
"custbankno": 14317,
"bankname": "邮政银行",
"bankprovince": "北京",
"bankcity": "台州市",
"exchangeflag": "1",
"exchangerate": "1.33",
"bankaccountno": "ohujfqh14929660006.607274295739865"
},
{
"payway": "1",
"capitalallocationbillno": "krvzzllo76796160000.4140002552705546",
"summaryno": "ZB2227681500186",
"exchangecurrencycode": "1",
"exchangeamount": "20029.50",
"currencycode": "2",
"amount": "48202.34",
"operatetime": "2019-09-20",
"customercode": "ICHN0347",
"customername": "陈磊",
"paymentstatus": "6",
"operratorname": "谭秀兰",
"fastflag": "1",
"tabperiod": "1973-06-10",
"memo": "整同去过长图照",
"paymenttime": "1996-04-02",
"in_amount": "2976.90",
"in_currency": "3",
"paymentmode": "2",
"visible": true,
"collpayflag": "1",
"accountno": "355081500180",
"backinfo": "务并级器个最以点建",
"viewPDF": "查看PDF",
"accountname": "只矿物验标别压见准回步公司",
"inpaymentmethod": "2",
"custaccountno": "9876iuxg42",
"custbankno": 14317,
"bankname": "农业银行",
"bankprovince": "江苏省",
"bankcity": "辽阳市",
"exchangeflag": "0",
"exchangerate": "1.33",
"bankaccountno": "kfnapcv5929660006.607274295739865"
}
],
"info": {
"payway": "1",
"capitalallocationbillno": "tvi18257460003.878545153676105",
"summaryno": "ZB2978681500186",
"exchangecurrencycode": "1",
"exchangeamount": "5533.58",
"currencycode": "2",
"amount": "23014.22",
"operatetime": "2017-04-09",
"customercode": "ICHN0345",
"customername": "曹娟",
"paymentstatus": "6",
"operratorname": "赖娜",
"fastflag": "0",
"tabperiod": "1971-12-13",
"memo": "使单导作等素力程",
"paymenttime": "2001-06-07",
"in_amount": "4940.62",
"in_currency": "3",
"paymentmode": "1",
"visible": true,
"collpayflag": "0",
"accountno": "296481500180",
"backinfo": "南对比记要维比了门",
"viewPDF": "查看PDF",
"accountname": "器世少党很本基子规强具公司",
"inpaymentmethod": "2",
"custaccountno": "43497wpg78",
"custbankno": 22353,
"bankname": "农业银行",
"bankprovince": "广西壮族自治区",
"bankcity": "洛阳市",
"exchangeflag": "1",
"exchangerate": "2.05",
"bankaccountno": "togbhrut77906560004.2015239222178735"
}
}
进入到改文件夹的目录,打开终端运行指令
json-server --watch db.json
默认端口是3000,可以修改,可参考官网。
出现以下这种说明服务启动成功。
PS D:\work\dorado\aaaaa\json-server-demo> json-server --watch db.json
–watch/-w can be omitted, JSON Server 1+ watches for file changes by default JSON Server started on PORT :3000 Press CTRL-C to stop
Watching db.json…♡( ◡‿◡ )
Index: http://localhost:3000/
Static files: Serving ./public directory if it exists
Endpoints: http://localhost:3000/list http://localhost:3000/info
遇到的问题:
1.
import { parseArgs } from 'node:util';
^^^^^^^^^
SyntaxError: The requested module 'node:util' does not provide an export named 'parseArgs'
最后发现是:node版本太低了,我原先用的是node 16.14.0 改为node 18.12.0就可以了。
参考文章
我是用的nvm管理的node版本,很方便,建议大家也用此工具管理node版本
nvm切换高版本node后,需重新安装下json-server。启动服务即可。
ending。