Mock: json-server

安装

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

你可能感兴趣的:(Mock: json-server)