通过 NodeJS 到本地主机后端的代理 EventSource (SSE) 连接

问题描述 投票:0回答:1

我在通过 node-http-proxy v1.18.1 进行 EventSource 连接时遇到问题。连接在开发中工作,来自 create-react-app, proxy: ,但是在部署到生产时,连接失败并出现以下浏览器(chromium)错误:

EventSource's response has a MIME type ("text/html") that is not "text/event-stream". Aborting the connection.

我的 server.js 中创建代理的部分是:

const httpProxy = require('http-proxy');

var app = express();
var apiProxy = httpProxy.createProxyServer({secure: false});

app.use(express.static(root));
app.use(compression());

/* proxy api calls over to port 9047 */
app.all('/api/*', (req, res) => {
   apiProxy.web(req,res, { target: 'https://localhost:9047' });
});

React/NodeJS 似乎返回到浏览器的 HTML 文档而不是 EventStream 数据是:

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8"/>
        <link rel="icon" href="/icons/favicon.ico"/>
        <meta name="viewport" content="width=device-width,initial-scale=1"/>
        <meta name="theme-color" content="#000000"/>
        <meta name="description" content="Web site created using create-react-app"/>
        <link rel="apple-touch-icon" href="/icons/threaded_icon_57x57.png"/>
        <link rel="manifest" href="/manifest.json"/>
        <title>My App</title>
        <script defer="defer" src="/static/js/main.8ebfafc7.js"></script>
        <link href="/static/css/main.8c23b3de.css" rel="stylesheet">
    </head>
    <body>
        <noscript>You need to enable JavaScript to run this app.</noscript>
        <div id="root"></div>
    </body>
</html>

我还尝试使用 http-proxy-middleware 库,它包装了 node-http-proxy 模块。结果完全相同。

本地主机上的后端服务器(springboot)似乎没有收到调用。它似乎没有在 NodeJS/Express 中正确代理。

我 100% 确定我的后端(springboot)服务器返回正确的响应类型,因为它在开发中工作,并且使用非 React/非 Express 客户端在生产中工作。有什么建议吗?

node.js reactjs express server-sent-events node-http-proxy
1个回答
0
投票

根本问题与事件流注册的 URL 路径与 express .all() 调用中匹配的模式不同有关。

© www.soinside.com 2019 - 2024. All rights reserved.