【发布时间】:2021-03-02 02:34:35
【问题描述】:
我有我想以 pdf 格式导出的模板。我使用的引擎是PUG的。我有这个文件,我在其中进行 GET 调用以导出 pdf。 但是当我启动我的服务器时 - 节点 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
$
我不确定为什么会发生此错误。任何帮助表示赞赏。
【问题讨论】:
标签: javascript node.js google-chrome puppeteer node-modules