【问题标题】:nodejs proxy timing and async behaviornodejs 代理计时和异步行为
【发布时间】:2015-04-21 12:34:17
【问题描述】:

我对以下代码有疑问: 我需要确认在 proxyRes 事件中完成的所有处理 将异步完成,其中的所有处理时间都不会影响 代理时间。

提前感谢您在此方面的帮助

    var server = http.createServer(function (req, res) {
    console.time();
    var proxy = httpProxy.createProxyServer();
    proxy.web(req, res, {
      target: 'https://gdev.sserver.be/api/host1/account',
    });
    console.timeEnd();

    proxy.on('proxyRes', function (proxyRes, req, res) {
        //console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2));
        proxyRes.on('data', function (chunk) {
                console.log('resp => ' + chunk);
                connection.query('INSERT INTO test SET ?', {content: chunk}, function(err, result) {
                        if (err) throw err;
                        console.log('writing in db');
                });
        });
        proxy.close();
        proxy = null;
    });
}).listen(3000);

【问题讨论】:

  • 我的回答对你有帮助吗?

标签: node.js asynchronous http-proxy node-http-proxy


【解决方案1】:

是的,您的 proxyRes 处理程序的内容似乎是异步的。当数据库查询仍在做它需要做的事情时,代理就会发生,所以你是对的,它不会影响代理时间。

【讨论】:

    猜你喜欢
    • 2013-03-19
    • 2013-07-04
    • 2022-01-12
    • 1970-01-01
    • 2017-05-13
    • 2017-11-14
    • 2019-10-01
    • 2011-09-08
    • 2015-10-30
    相关资源
    最近更新 更多