Mat*_*zke 5 proxy http-proxy node.js typescript http-proxy-middleware
在http-proxy-middleware库中,文档指出您可以使用该target选项来指定要代理请求的位置。但是,它们还允许您使用该router选项来指定将用于在运行时从功能上解析目标的函数。
文档:https : //www.npmjs.com/package/http-proxy-middleware
我正在使用 TypeScript,如果我查看代理的声明文件,我可以看到:
您可以在此处看到router和target都可以为空。我的假设是,如果你使用一个,另一个可以省略,但你总是至少需要一个。
但是,如果我使用这样的router属性:
app.use("/pipe", proxy({
changeOrigin: true,
router: (req: IIncomingMessageWithCookies) => {
return "https://www.google.com";
}
}));
Run Code Online (Sandbox Code Playgroud)
并省略target,然后在运行时我收到此错误:
> node ./dist/app.js
C:\SkyKick\SkyKick.SEWR\src\node_modules\http-proxy-middleware\lib\config-factory.js:43
throw new Error(ERRORS.ERR_CONFIG_FACTORY_TARGET_MISSING)
^
Error: [HPM] Missing "target" option. Example: {target: "http://www.example.org"}
at Object.createConfig (C:\SkyKick\SkyKick.SEWR\src\node_modules\http-proxy-middleware\lib\config-factory.js:43:11)
at new HttpProxyMiddleware (C:\SkyKick\SkyKick.SEWR\src\node_modules\http-proxy-middleware\lib\index.js:17:30)
at module.exports (C:\SkyKick\SkyKick.SEWR\src\node_modules\http-proxy-middleware\index.js:4:10)
at Object.<anonymous> (C:\SkyKick\SkyKick.SEWR\src\dist\app.js:8:18)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
Run Code Online (Sandbox Code Playgroud)
我意识到我可以将几乎任何东西放入其中target并且它会运行得很好,我的router函数实际上是定义目标代理的东西。这只是库的错误还是我误解了这两个选项的用途?
包括target和router财产。该router酒店可根据特定要求重新定位option.target。
import express = require('express');
import proxy = require('http-proxy-middleware');
const app = express();
app.use('/api', proxy({
target: 'http://www.github.com',
changeOrigin: true,
router: function (req: IncomingMessage) {
return 'http://www.stackoverflow.com'
}
}));
app.listen(3000);
Run Code Online (Sandbox Code Playgroud)
该target属性在类型中是可选的,因为当我们使用如下所示的简写Config时,它允许为空:
app.use('/api', proxy('http://www.github.com',
{
changeOrigin: true,
router: function (req: IncomingMessage) {
return 'http://www.stackoverflow.com'
}
}
));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6462 次 |
| 最近记录: |