首页 > 解决方案 > 使用 create-react-app 时无效的主机头

问题描述

我正在使用 creat-react-app 开发一个应用程序,其中我还使用 Heroku 和 Netlify 来部署该应用程序。我让它工作正常,但突然在请求的资源错误中出现 No 'Access-Control-Allow-Origin' 标头。它还在后面的行上显示“网络错误”。

当我去 Heroku 并打开应用程序时,我得到了 INVALID HOST HEADER。

当我在 heroku 和 netlify 上部署时,它们都成功并且没有显示错误,所以我不确定我做错了什么。

我在 package.json 中使用代理:

"proxy": "http://localhost:5000",

和我的 server/index.js 文件:

const app = express();
dotenv.config();

app.use(cors());
app.use(bodyParser.json({ extended: true }));
app.use('/movements', movementRoutes);

app.get('/', (req, res) => {
    res.send('Your one rep maxes, together');
});

const PORT = process.env.PORT || 5000;

mongoose.connect( process.env.CONNECTION_URL )
    .then(() => app.listen(PORT, () => console.log(`Server running on port ${PORT}`)))
    .catch((error) => console.log(error.message));

到目前为止,我尝试过的是:

删除代理

创建一个 .env.development 文件并添加 HOST=(my heroku app name),但这给了我一个“没有打开端口”的错误。

有人对我做错了什么有建议吗?

标签: herokuxmlhttprequest

解决方案


推荐阅读