vbenjs / vite-plugin-mock

A mock plugin for vite.use mockjs.
MIT License
625 stars 96 forks source link

生产环境下 async mock 提示错误 #58

Open yangjiandong opened 3 years ago

yangjiandong commented 3 years ago

env

"react": "^17.0.0",
"typescript": "^4.4.4",
"vite": "^2.6.14",
"vite-plugin-mock": "^2.9.6"

code

{
    url: "/auth/oauth/token",
    method: "post",
    rawResponse: async (req, res) => {
      console.log("[mock login, request]", req.headers);
      await waitTime(500);
      let body = "";
      await new Promise((resolve) => {
        req.on("data", (params) => {
          body += params;
        });
        req.on("end", () => resolve(undefined));
      });
      // body :  username=admin&password=123&grant_type=password

      const params = parse(body);
      console.log("[ mock ] body : ", params);
      let username = params["username"];
      console.log("[ mock ] username : ", username);
      if (username === "user400") {
        res.statusCode = 400;
        res.end(
          JSON.stringify({
            exception: "登录失败,密码错误,注意登录次数限制",
          })
        );
        return;
      }

      const user = users[username as keyof typeof users];
      console.log("[ mock ] user ", user);
      if (!user) {
        console.log("[ mock ] no user");
        res.statusCode = 401;
        res.end(
          JSON.stringify({
            code: "ERROR",
            exception: "登录失败,用户名或者密码错误",
          })
        );
        return;
      }
      res.end(JSON.stringify(token));
    },
  },

开发环境下正常,打包后生产环境下控制台显示

Uncaught (in promise) Error: no find login
    at C.interceptors.response.use.response (index.fde0683e.js:1)
    at async index.fde0683e.js:1
Broderick890 commented 1 year ago

+1