Mock: json-server

2022-03-10  本文已影响0人  疾风劲草ccy

安装

npm i -g json-server

新建/mock/db.json Restful 接口

{
  "list": []
}

新建/mock/middleware.js 非Restful接口

module.exports = (req, res, next) => {
  if (req.method === 'POST' && req.path === '/login') {
    if (req.body.username === 'cc' && req.body.password === '123456') {
      return res.status(200).json({
        user: {
          token: '123'
        }
      })
    } else {
      return res.status(400).json({
        message: '用户名或者密码错误'
      })
    }
  }
}

package.json

{
  scripts: {
        "json-server": "json-server mock/db.json --watch --port 3001 --middlewares mock/middleware.js"
  }
}

启动

npm run json-server
上一篇 下一篇

猜你喜欢

热点阅读