ngserve --proxy-config 与 NTLM 身份验证不起作用

gat*_*pia 5 node-http-proxy webpack webpack-dev-server angular-cli angular

我试图让 Angular cli 的内部网络服务器(我认为 webpack 使用node-http-proxy)与 NTLM 身份验证一起使用,但出现了问题。

我这样设置 webpack 代理:

// in packages.json
...
"scripts": {
    "start": "ng serve --proxy-config proxy.conf.json",
...
Run Code Online (Sandbox Code Playgroud)

proxy.config.json 的内容是:

{
  "/srv": {
    "target": "http://localhost/access_form",
    "logLevel": "debug",
    "auth": "LOGIN:PASS"
  }
}
Run Code Online (Sandbox Code Playgroud)

我试图将 onProxyRes 函数添加到 JSON 选项对象,但这无法启动网络服务器。

有人对这个设置有过运气吗?有什么指点吗?

jac*_*ton 5

我可以通过使用以下内容作为我的proxy.config.js文件来完成此工作,该文件可以传递给 angular-cli 工具,如下所示ng serve --watch --proxy-config proxy.config.js

var Agent = require("agentkeepalive");

var keepaliveAgent = new Agent({
    maxSockets: 100,
    keepAlive: true,
    maxFreeSockets: 10,
    keepAliveMsecs: 1000,
    timeout: 60000,
    keepAliveTimeout: 30000 // free socket keepalive for 30 seconds
});

var onProxyRes = function (proxyRes, req, res) {
    var key = 'www-authenticate';
    proxyRes.headers[key] = proxyRes.headers[key] && proxyRes.headers[key].split(',');
};

const PROXY_CONFIG = [
    {
        target: "http://localhost:12345",
        context: "/api",
        secure: false,
        changeOrigin: true,
        auth: "LOGIN:PASS",
        loglevel: "debug",
        onProxyRes: onProxyRes,
        agent: keepaliveAgent
    }
];
module.exports = PROXY_CONFIG;
Run Code Online (Sandbox Code Playgroud)

确保安装了 agentkeepalive 软件包:

npm install --save-dev agentkeepalive
Run Code Online (Sandbox Code Playgroud)

更多信息请参见:


小智 1

http-proxy-middleware issues 39中有一个部分解决方案,但它有一个问题:

var Agent = require('agentkeepalive');

{
  devServer: {
    '/api/*': {
      target: 'http://localhost:12121',
      logLevel: 'debug',
      agent: new Agent({
        maxSockets: 100,
        keepAlive: true,
        maxFreeSockets: 10,
        keepAliveMsecs:1000,
        timeout: 60000,
        keepAliveTimeout: 30000 // free socket keepalive for 30 seconds
     }),
     onProxyRes: proxyRes => {
        var key = 'www-authenticate';
        proxyRes.headers[key] = proxyRes.headers[key] && proxyRes.headers[key].split(',');
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这是讨论:https://github.com/chimurai/http-proxy-middleware/issues/39

包括我在内的一些用户收到异常“TypeError:cb 不是函数”。讨论引用了一个 nodejs/node 问题:“Uncaught TypeError using http.Agent in keep-alive mode #8650”,目前似乎尚未解决。

这是讨论: https: //github.com/nodejs/node/issues/8650