繁体   English   中英

在 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:\16pf\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:\16pf\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
$

我不确定为什么会发生此错误。 任何帮助表示赞赏。

您可以使用不支持可选 catch 绑定的旧 Node.js 版本。 查看支持列表: https : //node.green/#ES2019-misc-optional-catch-binding

只不过是一个 javascript 语法错误。 用 Nano 编辑 707 行:

nano +707 "E:\16pf\node_modules\puppeteer\lib\cjs\puppeteer\common\Page.js"

改变:

catch {

到:

catch(err) {

vsemozhebuty 的答案可能会更好,因为即使在您修复了上述 javascript 错误之后,您可能还需要修复更多才能使其正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM