代码拉取完成,页面将自动刷新
npm install json-server
json-server -h
json-server [options] <source>
选项:
--config, -c Path to config file [默认值: "json-server.json"]
--port, -p Set port [默认值: 3000]
--host, -H Set host [默认值: "0.0.0.0"]
--watch, -w Watch file(s) [布尔]
--routes, -r Path to routes file
--static, -s Set static files directory
--read-only, --ro Allow only GET requests [布尔]
--no-cors, --nc Disable Cross-Origin Resource Sharing [布尔]
--no-gzip, --ng Disable GZIP Content-Encoding [布尔]
--snapshots, -S Set snapshots directory [默认值: "."]
--delay, -d Add delay to responses (ms)
--id, -i Set database id property (e.g. _id) [默认值: "id"]
--quiet, -q Suppress log messages from output [布尔]
--help, -h 显示帮助信息 [布尔]
--version, -v 显示版本号 [布尔]
示例:
json-server db.json
json-server file.js
json-server http://example.com/db.json
https://github.com/typicode/json-server
json-server.json
{
"host": "0.0.0.0",
"port": "3003",
"watch": true
}
package.json
"scripts": {
"mock": "json-server mock.json"
}
npm run mock
http://localhost:3003
$.ajax({
type: 'get',
url: 'http://localhost:3003/news'
}
)
获取所有数据
$.ajax({
type: 'get',
url: 'http://localhost:3003/news/1'
}
)
获取所有数据 id = 1 的数据
$.ajax({
type: 'post',
url: 'http://localhost:3003/news',
data: {
"id": 3,
"title": "我是新加入的新闻",
"date": "2019-09-26",
"likes": 0,
"views": 0
}
}
)
再次 GET 访问可获取新增加数据
$.ajax({
type: 'put',
url: 'http://localhost:3003/news/1',
data: {
"title": "曹县宣布昨日晚间登日失败",
"date": "2016-08-12",
"likes": 55,
"views": 100086
}
}
)
再次 GET 访问可获取修改数据
同 PUT
$.ajax({
type: 'delete',
url: 'http://localhost:3003/news/1'
}
)
再次 GET 访问 id = 1 的数据被删除
pm2 是一个带有负载均衡功能的Node应用的进程管理器
package.json
{
"scripts": {
"split": "json-server split-file/server.js -r split-file/routes.json"
}
}
npm run split
pm2 start npm --name mock -- run split
pm2 list
pm2 restart xxx
pm2 stop xxx
pm2 delete xxx
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。