美文网首页
json-server 中间件

json-server 中间件

作者: RickyWu585 | 来源:发表于2022-01-04 10:52 被阅读0次
    • 简易模拟登陆中间件实现(这不就是集成了express的功能嘛):
    module.exports = (req, res, next) => {
      if (req.method === 'POST' && req.path === '/login') {
        if (req.body.username === 'jira' & req.body.password === '123456') {
          return res.status(200).json({
            user: {
              token: '123'
            }
          })
        } else {
          return res.status(400).json({
            message: '用户名或密码错误'
          })
        }
      }
      next()
    }
    
    • package.json修改:
    "json-server": "json-server __json_server_mock__/db.json --watch --port 3010 --middlewares ./__json_server_mock__/middleware.js"
    

    相关文章

      网友评论

          本文标题:json-server 中间件

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