我有一个React客户端和一个Springboot服务器。我让他们分开跑。在我的package.json中,我将代理设置为localhost:8080。但是当我部署到Heroku时,我得到了Invalid Header。
如果我删除它,那么当客户机有/api/auth这样的url时,它将尝试在前端服务器上找到它,而不是使用后端服务器。
已经阅读了几种可能的解决方案,例如用HOST=my-server.herokuapp.com创建一个HOST=my-server.herokuapp.com文件,以及安装http-proxy-middleware和添加一个名为setupProxy.js的文件
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function(app) {
app.use(
'/api',
createProxyMiddleware({
target: 'http://my-server.herokuapp.com',
changeOrigin: true,
})
);
};这些都解决不了这个问题。
我见过webpack的名字,但我对Javascript框架并不熟悉,所以我对此并不熟悉。我只是有反应,我从npm start开始
发布于 2022-02-27 16:25:37
我已经不在这个项目上了,我不记得我为这个特定的问题做了什么。但是经过大量的研究,我认为最好的方法是在一个服务器上同时运行前端和后端。我使用前端-maven-插件将React组件构建到Springboot可以提供服务的位置。我使用heroku-maven-plugin部署到heroku。
这是我的pom的相关部分:
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>${frontend-maven-plugin.version}</version>
<configuration>
<nodeVersion>${node.version}</nodeVersion>
<workingDirectory>${frontend-src-dir}</workingDirectory>
<installDirectory>${frontend-src-dir}</installDirectory>
</configuration>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
</execution>
<execution>
<id>npm run build</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>run build</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.heroku.sdk</groupId>
<artifactId>heroku-maven-plugin</artifactId>
<version>3.0.3</version>
<configuration>
<appName>$heroku-app-name</appName>
</configuration>
</plugin>
</plugins>https://stackoverflow.com/questions/69792143
复制相似问题