【发布时间】:2021-08-13 14:03:26
【问题描述】:
我正在尝试从调用 api 的 html 模板创建 pdf,所以我正在使用 puppeteer。
问题是,当我尝试从 puppeteer 启动创建新页面时,它不起作用,并且它还重新启动服务器,我不知道为什么。我在 localhost 上工作,puppeteer 的版本是 ^9.1.1
这是我通过单击 href 链接调用 api 时的代码
export default async (templateName, { totalPages, currentPage, ...data }) => {
const browser = await puppeteer.launch({
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
],
})
const html = await handlebars(
templateName,
{
...data,
},
)
const tmpId = uuidV4()
const tempFolder = path.join(__dirname, '../', tmpId)
const tempFilePath = path.join(tempFolder, 'index.html')
await mkdir(tempFolder)
await writeFile(tempFilePath, html)
await copyFolder(
path.join(__dirname, '../templates', 'quote', 'assets'),
path.join(tempFolder, 'assets'),
)
console.log("here")
const page = await browser.newPage()
console.log("after")
// the code to make the pdf...
return pdf
}
这里是 api 服务器:
import http_errors from 'http-errors';
const { NotFound } = http_errors;
import 'dotenv/config.js'
import './config/init_redis.js'
import express, { json, urlencoded } from "express"
import htmltopdfRouter from "./html_to_pdf/routes/index.js"
const app = express();
import cookieParser from 'cookie-parser'
import cors from 'cors'
app.use(json())
app.use(cookieParser())
app.use(cors({ credentials: true, origin: "http://localhost:8081" }));
app.use(urlencoded({ extended: true }))
app.use("/api", htmltopdfRouter)
app.use(async (req, res, next) => {
next(NotFound())
})
app.use((err, req, res, next) => {
res.status(err.status || 500)
res.send({
success: err.status,
message: err.message,
})
})
const port = process.env.APP_PORT || 4000;
app.listen(port, () => {
console.log("Server up and running on PORT :", port);
});
【问题讨论】:
-
您是否尝试使用某些 API 端点调用 puppeteer?还有你的 app.js 里面有什么。您是否将此代码作为 app.js 的一部分?
-
谢谢@dangi13的回复,请看问题编辑
-
你正在调用你的 puppeteer 中的某些东西,它以某种方式修改了你文件夹中的代码/文件,导致 nodemon 由于更改而重新启动。
-
不确定,但我想您可以尝试排除 nodemon 以不监视某些特定文件夹,使其不会重新启动。 stackoverflow.com/questions/24120004/nodemon-exclusion-of-files也许这可以帮助
-
你看到你是如何创建目录和写临时文件的了吗?它正在触发 nodemon