A
A
Artem2020-11-15 00:14:33
Node.js
Artem, 2020-11-15 00:14:33

How once having created object of the browser it is constant to address it?

Hello.

I have the following in stock:

const express = require('express')
const puppeteer = require('puppeteer');
const app = express()
const port = 3000


app.get('/render', (req, res) => {
    (async (req) => {
        const browser = await puppeteer.launch();
        const page = await browser.newPage();
        await page.goto(req.query.url, {waitUntil: 'networkidle2'});
        await page.emulateMediaType('screen')
        await page.setViewport({
            width: 400,
            height: 1080,
            deviceScaleFactor: 1,
        });
        console.log(req.query.url)
        await page.pdf({
            path: 'hn.pdf',
            format: 'A4',
            margin: {
                top: 15,
                bottom: 15,
                right: 15,
                left: 15
            }
        });
        console.log('rendered')

        await browser.close();
    })(req)

    res.send('123')
})

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})


I want to speed up rendering by not creating a browser object for each request, but using it as a resource cached in RAM.

Moving the creation of the browser object behind the /render handler results in a TypeError: browser.newPage is not a function
Tell me what to do?

PS don't kick too hard, I don't really understand how to use all the features of node.js

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question