Reactjs 如有规定,“;委托书;in package.json必须是字符串
我希望在我的react客户端中使用代理,my package.json包含:Reactjs 如有规定,“;委托书;in package.json必须是字符串,reactjs,Reactjs,我希望在我的react客户端中使用代理,my package.json包含: ... "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" }, "proxy": { "/auth/google": { "target
...
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"proxy": {
"/auth/google": {
"target": "http://localhost:5000"
}
},
...
但当我运行它时,我得到了错误
When specified, "proxy" in package.json must be a string.
[1] Instead, the type of "proxy" was "object".
[1] Either remove "proxy" from package.json, or make it a string.
我试图转换为字符串,没有错误,但代理不起作用
"proxy": "http://localhost:5000"
我的App.js
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>hey there</p>
<a href="/auth/google">Sign In With Google</a>
</header>
</div>
嘿
我认为这是“创建反应应用程序”的问题
你可以去
迁移到新的代理处理方法
简而言之,您只需要安装一个名为“http代理中间件”的新库
然后创建一个新文件“src/setupProxy.js”,并键入
const proxy = require('http-proxy-middleware');
module.exports = function(app) {
app.use(proxy('/auth/google', { target: 'http://localhost:5000/' }));
};
希望这能解决你的问题,快乐黑客 您所面临的问题是因为CRA v2 首先,如果只是在代理中使用普通字符串,则不需要任何额外的配置。但是,当您使用对象时,您正在使用高级配置 因此,您必须遵循下列步骤:
npmi--save http代理中间件来安装http代理中间件
“代理”:{
“/auth/google”:{
“目标”:http://localhost:5000"
}
}
constproxy=require('http-proxy-middleware');
module.exports=函数(应用程序){
app.use(proxy('/auth/google',,
{目标:'http://localhost:5000/' }
));
}
有关更多信息,请首先检查,使用npm或Thread安装http代理中间件:
$ npm install http-proxy-middleware --save
$ # or
$ yarn add http-proxy-middleware
$ npm install http-proxy-middleware --save
$ # or
$ yarn add http-proxy-middleware
接下来,创建src/setupProxy.js并在其中放置以下内容:
const proxy = require('http-proxy-middleware')
module.exports = function(app) {
// ...
}
const proxy = require('http-proxy-middleware')
module.exports = function(app) {
// ...
}
现在,逐个迁移代理对象中的每个条目,例如:
"proxy": {
"/api": {
"target": "http://localhost:5000/"
},
"/*.svg": {
"target": "http://localhost:5000/"
}
}
"proxy": {
"/api": {
"target": "http://localhost:5000/"
},
"/*.svg": {
"target": "http://localhost:5000/"
}
}
将条目放入src/setupProxy.js
中,如下所示:
const proxy = require('http-proxy-middleware')
module.exports = function(app) {
app.use(proxy('/api', { target: 'http://localhost:5000/' }))
app.use(proxy('/*.svg', { target: 'http://localhost:5000/' }))
}
你现在也可以使用完全自定义的逻辑了!
在客户端(React app)中创建了一个名为
src/setupProxy.js
确保重新启动服务器。由于您正在处理源目录之外的文件,因此需要重新启动package.json文件
将“http代理中间件”安装到客户端,“而不是服务器内部”
在client/src/目录中添加setupProxy.js。
(应该是这样的:client/src/setupProxy.js)
将下面的行添加到它
const proxy = require("http-proxy-middleware");
module.exports = app => {
app.use(proxy("/auth/google", { target: "http://localhost:5000/" }));
};
就这样,进入您的google开发控制台,将localhost:3000/auth/google/callback添加到您的项目中。将代理更改为类似的内容,并希望它能像我一样工作
“proxy”:“在我的例子中,我不需要src/setupProxy.js… 我是用axios做的 检查节点库是否有:http代理中间件是可选的,我不需要它 试着重新启动服务器端,就这样强> 添加到检查:
componentDidMount(){
axios.get('/api/path-you-want').then(response=>{
console.log(response)
})
}
将高级代理配置移动到src/setupProxy.js
此更改仅适用于在v1中使用高级代理配置的个人
要检查是否需要执行操作,请在package.json中查找代理密钥。然后,按照下表进行操作
我在package.json中找不到代理密钥
不需要采取任何行动!
代理的值是一个字符串(例如)
不需要采取任何行动!
代理的值是一个对象
按照下面的迁移说明操作。
如果您的代理是一个对象,则表示您正在使用高级代理配置
同样,如果代理字段是字符串,例如,您不需要执行任何操作。此功能仍受支持,并具有相同的行为
首先,使用npm或Thread安装http代理中间件:
$ npm install http-proxy-middleware --save
$ # or
$ yarn add http-proxy-middleware
$ npm install http-proxy-middleware --save
$ # or
$ yarn add http-proxy-middleware
接下来,创建src/setupProxy.js并在其中放置以下内容:
const proxy = require('http-proxy-middleware')
module.exports = function(app) {
// ...
}
const proxy = require('http-proxy-middleware')
module.exports = function(app) {
// ...
}
现在,逐个迁移代理对象中的每个条目,例如:
"proxy": {
"/api": {
"target": "http://localhost:5000/"
},
"/*.svg": {
"target": "http://localhost:5000/"
}
}
"proxy": {
"/api": {
"target": "http://localhost:5000/"
},
"/*.svg": {
"target": "http://localhost:5000/"
}
}
将条目放入src/setupProxy.js,如下所示:
const proxy = require('http-proxy-middleware')
module.exports = function(app) {
app.use(proxy('/api', { target: 'http://localhost:5000/' }))
app.use(proxy('/*.svg', { target: 'http://localhost:5000/' }))
}
你现在也可以使用完全自定义的逻辑了!这以前是不可能的
它成功了。这与create react应用程序版本2中的一个错误有关
app.use(
'/api',
proxy({ target: 'http://www.example.org', changeOrigin: true })
);
changeOrigin:true
快跑
$ npm install react-scripts@next --save
$ # or
$ yarn add react-scripts@next
答案载于:
如果您需要代理请求并重写URL,例如
localhost:3000/api/backend/some/method
到https://api-server.example.com/some/method
,您还需要使用路径重写
选项:
const{createProxyMiddleware}=require(“http代理中间件”);
module.exports=函数(应用程序){
应用程序使用(
“/api/backend”,
CreateProxy中间件({
目标:“https://api-server.example.com",
来源:对,
路径重写:{
“^/api/backend”:”,
},
})
);
};
以下几点对我很有用:
从package.json中删除“proxy”
在客户端目录中安装“http代理中间件”。为此,将cd放入客户机目录并运行“npm i——保存http代理中间件”。然后,在客户端的src目录中创建一个名为“setupProxy.js”的新文件。在此文件中放置以下代码:
const { createProxyMiddleware } = require('http-proxy-middleware');
const proxy = require('http-proxy-middleware');
module.exports = function(app) {
app.use(createProxyMiddleware('/api/', // replace with your endpoint
{ target: 'http://localhost:8000' } // replace with your target
));
}
重新启动服务器,您就可以开始了。对于2020年的用户,
通过在client
文件夹中键入npm i--save http proxy middleware
安装http代理中间件
从package.json
中删除条目:
"proxy": {
"/auth/google": {
"target": "http://localhost:5000"
}
}
现在为您的代理创建一个安装文件。您应该在客户端的src文件夹中将其命名为setupProxy.js
,并键入以下代码:
const { createProxyMiddleware } = require("http-proxy-middleware");
module.exports = function (app) {
app.use(
createProxyMiddleware("/auth/google", { target: "http://localhost:5000/" })
);
};
备注:您不需要在
server.js
或index.js
中的任何位置包含setupProxy.js
。只需复制并粘贴。在src文件夹中创建一个setupProxy.js文件,并复制粘贴以下代码
const { createProxyMiddleware } = require("http-proxy-middleware");
module.exports = function (app) {
app.use(
createProxyMiddleware("/auth/google", {
target: "http://localhost:5000/",
})
);
};
此时,我正在使用React 16.8.13这很好: 1-从
package.json
文件中删除“代理”:{***}
2-键入npm安装http-p