http-proxy-middleware 返回 404

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

我有一个 React 应用程序,我正在添加

http-proxy-middleware

我的目标是将所有请求重定向到

/recipes
/users
到我的新代理中间件,因此我向
app.js
添加了以下代码行:

import { createProxyMiddleware } from 'http-proxy-middleware';

const setupProxy = () => {
    return createProxyMiddleware(['/recipes','/user'], {target: 'http://localhost:3001'});
}

但是当我运行时,我收到错误 404。

浏览器记录器说

[HPM] Proxy created: /recipes,/user  -> http://localhost:3001
xhr.js:187 GET http://localhost:3000/recipes 404 (Not Found)

看起来它完全忽略了中间件!

没有

http-proxy-middleware
,但仅使用
"proxy":  "http://localhost:3001",
中的
package.json
语句,应用程序运行不会出现问题。

有什么想法吗?

reactjs proxy http-status-code-404 http-proxy-middleware
1个回答
0
投票

就我而言,我将 import 更改为 require。我不知道为什么,但它有效。

从'http-proxy-middleware'导入{createProxyMiddleware}; 到 const { createProxyMiddleware } = require('http-proxy-middleware');

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