json-server的安装与使用

json-server 详解

JSON-Server 是一个 Node 模块,运行 Express 服务器,你可以指定一个 json 文件作为 api 的数据源。

安装json-server

npm install -g json-server

创建文件夹初始化pack.json

npm init

启动 json-server

json-server可以直接把一个json文件托管成一个具备全RESTful风格的API,并支持跨域、jsonp、路由订制、数据快照保存等功能的 web 服务器。

创建一个db.json文件的内容:

{
  "users": [
    {
      "name": "小hua",
      "phone": "123456",
      "email": "1323@qq.com",
      "education": "一本",
      "graduationschool": "中国农业大学",
      "job": "技术人员",
      "profile": "是甘肃农村的一名孩子",
      "id": 2
    },
    {
      "name": "xiao",
      "phone": "6767889",
      "email": "123675@qq.con",
      "education": "本科",
      "graduationschool": "交大",
      "job": "文员",
      "profile": "喜欢玩",
      "id": 3
    },
    {
      "name": "小明",
      "phone": "23w43",
      "email": "324532",
      "education": "sadxcv",
      "graduationschool": "sdbcv",
      "profile": "dxvc b",
      "id": 4
    }
  ],
  "companys": [
    {
      "id": 1,
      "name": "Apple",
      "descript": "Apple is good!"
    },
    {
      "id": 2,
      "name": "Microft",
      "descript": "Microft is good!"
    },
    {
      "id": 3,
      "name": "Huawei",
      "descript": "Huawei is good!"
    }
  ]
}

例如以下命令,把db.json文件托管成一个 web 服务。

json-server --watch --port 53000 db.json

在pack.json 中修改

"scripts": {
"test": "json-server --watch --port 53000 db.json",
"remote":"json-server http://jsonplaceholder.typicode.com/db"
}

如果不添加--port 53000 默认端口为3000
启动脚本

npm run test

输出类似以下内容,说明启动成功。

\{^_^}/ hi!

Loading db.json
Done

Resources
http://localhost:53000/course

Home
http://localhost:53000

Type s + enter at any time to create a snapshot of the database
Watching...

了解更多json-server

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值