首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何代理从反应性客户端到Springboot服务器

如何代理从反应性客户端到Springboot服务器
EN

Stack Overflow用户
提问于 2021-11-01 02:41:49
回答 1查看 298关注 0票数 0

我有一个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的文件

代码语言:javascript
复制
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开始

EN

回答 1

Stack Overflow用户

发布于 2022-02-27 16:25:37

我已经不在这个项目上了,我不记得我为这个特定的问题做了什么。但是经过大量的研究,我认为最好的方法是在一个服务器上同时运行前端和后端。我使用前端-maven-插件将React组件构建到Springboot可以提供服务的位置。我使用heroku-maven-plugin部署到heroku。

这是我的pom的相关部分:

代码语言:javascript
复制
            <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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69792143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档