0

I'm trying to create a pdf from html template calling an api, so i'm using puppeteer.

The problem is, when i try to create a new page from puppeteer launch, it doesn't work and also it restarts the server and I don't know why. I'm working on localhost and the version of puppeteer is ^9.1.1

Here is the code when I call the api by clicking a href link

    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
 }

Here is the api server:

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);
});

enter image description here

July
  • 516
  • 1
  • 7
  • 25
  • 1
    Are you trying to invoke the puppeteer using some API endpoint? And also what is inside your app.js. Do you have this code as a part of app.js? – dangi13 May 25 '21 at 10:38
  • Thank you @dangi13 for you reply, please look at the question edited – July May 25 '21 at 10:47
  • 1
    you are calling something in your puppeteer that is somehow modifying codes/files in your folder, causing nodemon to restart due to changes. – Someone Special May 25 '21 at 11:11
  • @SomeoneSpecial as a matter of fact.. yes! please see the edited question... So it is not possible using nodemon? Thank you – July May 25 '21 at 11:26
  • 1
    not sure but i guess you can try excluding nodemon to not monitor some specific folder so that it does not restart. https://stackoverflow.com/questions/24120004/nodemon-exclusion-of-files Maybe this can help – dangi13 May 25 '21 at 11:32
  • 1
    Did you see how you are making directories and writing temporary files? It's triggering nodemon – Someone Special May 25 '21 at 11:36

1 Answers1

2

You are writing codes that creates directory and temp files which triggers nodemon.

You can exclude those folders by editing your start script.

Example

nodemon --ignore 'tmp/*' index.js

Nodemon - exclusion of files

Someone Special
  • 12,479
  • 7
  • 45
  • 76
  • I have added "start": "nodemon --ignore './tmp/*' app.js" on package.json but it not working. api_folder-> tmp(folder),package.json are in the same level. it says [nodemon] starting `node app.js`, i don't know if it has to output the ignore code... – July May 25 '21 at 11:51
  • 1
    it's silent ignore – Someone Special May 25 '21 at 12:55