首页 > 解决方案 > 部署在 Heroku 上时,令牌未保存在本地存储中

问题描述

我构建了一个在本地机器上运行良好的 MERN 应用程序。但是当我将它部署到 Heroku 时,它无法请求 api 并返回 400 Bad Request。它有一个注册/登录,当我输入详细信息时我注意到它没有将令牌保存在本地存储中。因此它无法访问私有路由 APIS。但是,本地存储是前端的一部分,那么为什么它在 Heroku 中的行为不同呢?

这是一个请求:

fetch("/api/auth", {
      method: "POST",
      headers: {
        "Content-type": "application/json",
      },
      body: JSON.stringify(data),
    })
      .then((res) => res.json())
      .then((data) => {
        if (data.token) {
          localStorage.setItem("token", data.token);
          setAuthStatus(true);
        }
      })
      .catch((err) => console.log(err));

我有这个作为代理: "proxy": "http://localhost:5000"

标签: node.jsreactjsmongodbherokuweb-deployment

解决方案


推荐阅读