NodeJS-错误:连接ECONNREFUSED 127.0.0.1:port(chrome-远程接口)

时间:2020-11-11 14:16:16

标签: node.js linux puppeteer chrome-devtools-protocol chrome-remote-debugging

我使用 chrome-launcher chrome-remote-interface 编写了脚本,以使用Chrome将网页保存为pdf。

它在Windows机器上没有任何问题,但是当我在CentOS 7上尝试时,出现以下错误,我不知道为什么。两者都使用Chrome v86。

在Windows上,我使用NodeJS v12.18.4 在Linux上,我尝试了v15.1和v12.19

SELinux状态:已禁用

我试图检查其他应用程序是否正在使用该端口,并且没有错误。

df.fillna(method='ffill',axis=1)

我的代码:

node:internal/process/promises:218
    triggerUncaughtException(err, true /* fromPromise */);
    ^

Error: connect ECONNREFUSED 127.0.0.1:43265
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1128:16) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 43265
}

如果您还有其他使用Chrome并将其缩放到0.7的方法,请告诉我。

谢谢

1 个答案:

答案 0 :(得分:0)

我尝试单独使用launchChrome()函数,发现这是问题所在。经过研究,我找到了解决方案。我必须在chromeLauncher.launch标志中添加“ --no-sandbox”。

下面是完整的代码:

const chromeLauncher = require('chrome-launcher');
const CDP = require('chrome-remote-interface');
const file = require('fs');
var check = 1;

(async function() {
    async function launchChrome() {
      return await chromeLauncher.launch({
        chromeFlags: [
          '--no-first-run',
          '--headless',
          '--disable-gpu',
          '--no-sandbox'
        ]
      });
    }

    const chrome = await launchChrome();
    const protocol = await CDP({
      port: chrome.port
    });

    const { DOM, Page, Emulation, Runtime } = protocol;

    await Promise.all([
      Page.enable(),
      Runtime.enable(),
      DOM.enable()
    ]);

    const { frameId } = await Page.navigate({ url: 'https://url/' });
    await Page.loadEventFired();
    const script1 = "window.status";
    while(check){
        var result = await Runtime.evaluate({
            expression: script1
        });
        if(result.result.value=='ready_to_print'){
            check = 0;
        }
    }

    let { data } = await Page.printToPDF({
      landscape: false,
      printBackground: true,
      scale: 0.7
    });

    file.writeFile('print.pdf', Buffer.from(data, 'base64'), 'base64', function(err) {
      if (err) {
        console.log(err);
      }

      protocol.close();
      chrome.kill();
    });
  })();