在 node.js 中实施 Puppeteer
Implementing Puppeteer in node.js
我有我的模板,我想将其导出为 pdf 格式。我用的引擎是PUG的。我有这个文件,我在其中进行 GET 调用以导出 pdf。
但是当我启动我的服务器时 - node index.js
我收到错误消息(在下面分享)
const express = require ('express');
var router = express.Router();
const puppeteer = require('puppeteer');
router.get('/export/html', (res,req) => {
res.render('template');
});
router. get('/export/pdf', (req, res) => {
(async () => {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://localhost:3000/export/html');
// Get the "viewport" of the page, as reported by the page.
const dimensions = await page.evaluate(() => {
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
deviceScaleFactor: window.devicePixelRatio
};
});
console.log('Dimensions:', dimensions);
await browser.close();
} catch(e) {
console.log(e);
}
})();
});
module.exports = router;
$ node index.js
E:pf\node_modules\puppeteer\lib\cjs\puppeteer\common\Page.js:707
catch {
^
SyntaxError: Unexpected token {
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (E:pf\node_modules\puppeteer\lib\cjs\puppeteer\common\Target.js:19:19)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
Shahrukh@DESKTOP-CQ0JNTJ MINGW64 /e/16pf
$
我不确定为什么会出现此错误。感谢任何帮助。
您可能使用不支持 optional catch binding. See support list: https://node.green/#ES2019-misc-optional-catch-binding
的旧 Node.js 版本
只不过是 javascript 语法错误。用 Nano 编辑第 707 行:
nano +707 "E:pf\node_modules\puppeteer\lib\cjs\puppeteer\common\Page.js"
变化:
catch {
收件人:
catch(err) {
vsemozhebuty 的回答可能更好,因为即使您修复了上述 javascript 错误,您可能还需要修复更多才能使其正常工作。
我有我的模板,我想将其导出为 pdf 格式。我用的引擎是PUG的。我有这个文件,我在其中进行 GET 调用以导出 pdf。 但是当我启动我的服务器时 - node index.js 我收到错误消息(在下面分享)
const express = require ('express');
var router = express.Router();
const puppeteer = require('puppeteer');
router.get('/export/html', (res,req) => {
res.render('template');
});
router. get('/export/pdf', (req, res) => {
(async () => {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://localhost:3000/export/html');
// Get the "viewport" of the page, as reported by the page.
const dimensions = await page.evaluate(() => {
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
deviceScaleFactor: window.devicePixelRatio
};
});
console.log('Dimensions:', dimensions);
await browser.close();
} catch(e) {
console.log(e);
}
})();
});
module.exports = router;
$ node index.js
E:pf\node_modules\puppeteer\lib\cjs\puppeteer\common\Page.js:707
catch {
^
SyntaxError: Unexpected token {
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (E:pf\node_modules\puppeteer\lib\cjs\puppeteer\common\Target.js:19:19)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
Shahrukh@DESKTOP-CQ0JNTJ MINGW64 /e/16pf
$
我不确定为什么会出现此错误。感谢任何帮助。
您可能使用不支持 optional catch binding. See support list: https://node.green/#ES2019-misc-optional-catch-binding
的旧 Node.js 版本只不过是 javascript 语法错误。用 Nano 编辑第 707 行:
nano +707 "E:pf\node_modules\puppeteer\lib\cjs\puppeteer\common\Page.js"
变化:
catch {
收件人:
catch(err) {
vsemozhebuty 的回答可能更好,因为即使您修复了上述 javascript 错误,您可能还需要修复更多才能使其正常工作。