[英]My React app is not registering setupProxy.js with http-proxy-middleware
I am trying to proxy to multiple API with setupProxy.js but its totally not working.我正在尝试使用 setupProxy.js 代理多个 API,但它完全不起作用。 Only way i can proxy request in dev now is setting
proxy
string in package.json but its not solving my problem since i need to proxy to multiple endpoints and with string i can use only one.我现在可以在 dev 中代理请求的唯一方法是在 package.json 中设置
proxy
字符串,但它不能解决我的问题,因为我需要代理到多个端点并且字符串我只能使用一个。
I am using "http-proxy-middleware": "^0.19.1"
我正在使用
"http-proxy-middleware": "^0.19.1"
My setupProxy.js looks like this:我的 setupProxy.js 看起来像这样:
var express = require('express');
var app = express();
var proxy = require('http-proxy-middleware');
var cors = require('cors')
module.exports = function() {
app.use(cors());
app.use(proxy('/api', { target: 'http://localhost:52992/' }));
app.use(proxy('/otherapi', { target: 'https://localhost:44309/' }));
app.get('/einreichung', function(req, res){
res.sendfile(__dirname + '/build/index.html');
});
app.use(express.static(__dirname + '/build'));
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
}
I have tried different modifications.我尝试了不同的修改。 Without module.exports, with app parameter in
function(app)
and without express
and app variables
but nothing works.没有 module.exports,在
function(app)
有 app 参数,没有express
和 app variables
但没有任何效果。
You must import the createProxyMiddleware
from http-proxy-middleware
correctly:您必须正确地从
http-proxy-middleware
导入createProxyMiddleware
:
var express = require('express');
var app = express();
var { createProxyMiddleware } = require('http-proxy-middleware');
var cors = require('cors');
https://create-react-app.dev/docs/proxying-api-requests-in-development https://create-react-app.dev/docs/proxying-api-requests-in-development
Try using the first arg passed to the anonymous function as app
尝试使用传递给匿名函数的第一个 arg 作为
app
module.exports = function(app) {
// your code...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.