美文网首页
Mock: json-server

Mock: json-server

作者: 疾风劲草ccy | 来源:发表于2022-03-10 12:23 被阅读0次

安装

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

      本文链接:https://www.haomeiwen.com/subject/dvdfdrtx.html