首页 > 解决方案 > 生产中的 Puppeteer 504 网关超时错误

问题描述

我已经使用 puppeteer 实现了 pdf 生成

const puppeteer = require("puppeteer");  
module.exports = {
   async generate(req, res) {
   let url = req.query.url;
   let name = req.query.name ? req.query.name : "file";
   const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
   const page = await browser.newPage();
   await page.goto(url, { waitUntil: "networkidle0" });
   const pdf = await page.pdf({ format: "A4" });
            
   await browser.close();
            
   res.set({
      "Content-Type": "application/pdf",
       "Content-Length": pdf.length,
     });
   res.setHeader("Content-disposition", `attachment; filename=${name}.pdf`);
       res.send(pdf);
   },
};

    

当我在 localhost 中尝试此操作并生成 PDF 时,上面的代码运行良好,但是当我将其移至生产环境时,它会继续加载并给出 504 网关错误,例如:https ://api.socialtools.me/core/html- to-pdf?url=https://apps.socialtools.me/preview/discount-coupon/613e0c6a9115bb003681958d/print/0&name=discount-coupon

谁能帮我解决这个问题。

标签: node.jspuppeteer

解决方案


推荐阅读