node.js - NodeJS - 错误:连接 ECONNREFUSED 127.0.0.1:port (chrome-remote-interface)
问题描述
我使用chrome-launcher和chrome-remote-interface制作了一个脚本,以使用 Chrome 将网页保存为 pdf。
它在我的 Windows 机器上运行没有任何问题,但是当我在 CentOS 7 上尝试它时,我收到以下错误,我不知道为什么。两者都使用 Chrome v86。
在 Windows 上我使用 NodeJS v12.18.4 在 Linux 上我尝试了 v15.1 和 v12.19
SELinux 状态:禁用
我试图检查其他应用程序是否正在使用错误中的端口并且没有。
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
}
我的代码:
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'
]
});
}
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();
});
})();
如果您有其他使用 Chrome 并缩放到 0.7 的方法,请告诉我。
谢谢
解决方案
我尝试单独使用 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();
});
})();
推荐阅读
- windows - windows操作系统如何确定应该将消息发布到哪个窗口
- c# - 如何解决后台任务中的服务?
- javascript - 如何在反应中处理多个反应选择块
- c++ - 未定义对“Base::Base(int, int)”的引用
- arrays - 如何从 JSON 中获取数据数组内部数组?
- database - 如何将 VARRAY 添加到 Oracle 对象
- javascript - 根据索引名称动态解析 JSON 数据
- reactjs - 当我尝试使用 next.js 运行我的服务器时,我不断收到无效的钩子调用,但是当其他人克隆我的 repo 时,它在他们的 repo 上渲染得很好
- go - jung-kurt/gofpdf TM 商标符号未正确呈现
- javascript - 允许在最新的 Chrome 中从 Javascript 聚焦窗口