node.js - 在包json中添加多个代理
问题描述
我为我的 react 应用程序使用了两台服务器,一台用于 express,另一台用于 create-react-app。因此,在 package.json 的反应端服务器中,我添加了:
"proxy": {
"/auth/google": {
"target": "http://localhost:5000"
}
},
我运行服务器并收到此错误:
When specified, "proxy" in package.json must be a string.
Instead, the type of "proxy" was "object".
Either remove "proxy" from package.json, or make it a string.
我该如何解决?如何添加代理?也许使用其他语法?
解决方案
在 create-react-app v2 中不推荐使用高级代理设置。如果您不使用字符串,而是像您的情况一样使用对象,则必须使用http-proxy-middleware并在src/文件夹中设置setupProxy.js文件。
1.首先,使用npm或Yarn安装http-proxy-middleware :
npm install http-proxy-middleware --save
# or
yarn add http-proxy-middleware
2. 然后,创建src/setupProxy.js并添加以下内容:
const proxy = require('http-proxy-middleware')
module.exports = function(app) {
app.use(proxy('auth/google', { target: 'http://localhost:5000/' }))
}
有关此问题的更多信息:将高级代理配置移动到 src/setupProxy.js
推荐阅读
- java - 试图弄清楚如何使用“Date()”进行分配
- user-interface - Delphi 10.2 Tokyo 表单中 Timer 更新的滚动 TreeView 冻结了 Windows 10 中的表单
- javascript - PIXI.Application 不是构造函数 [PIXIJS]
- laravel - 试图删除用户。不允许获取方法
- java - Tenured full, Eden empty: 物体可以移动到 Eden 吗?
- python - 在保持形状的同时将点云下采样到特定数量的点
- angular - Angular 6中字段的自定义所需验证
- php - 如何使用 TwitterOAuth PHP 编写推文?
- c# - 如何在 C# 中加入 LINQ to SQL 中不同表中的多个参数
- corda - 如何在 Corda 中使用 Oracle 服务签署交易?