Puppeteer doesn't close browser

后端 未结 6 1984
逝去的感伤
逝去的感伤 2021-02-13 20:15

I\'m running puppeteer on express/node/ubuntu as follow:

var puppeteer = require(\'puppeteer\');
var express = require(\'express\');
var router = express.Router(         


        
6条回答
  •  误落风尘
    2021-02-13 20:35

    I ran into the same issue and while your shelljs solution did work, it kills all chrome processes, which might interrupt one that is still processing a request. Here is a better solution that should work.

    var puppeteer = require('puppeteer');
    var express = require('express');
    var router = express.Router();
    
    router.get('/', function (req, res, next) {
        (async () => {
            await puppeteer.launch({ headless: true }).then(async browser => {
                const page = await browser.newPage();
                url = req.query.url;
                await page.goto(url);
                let bodyHTML = await page.evaluate(() => document.body.innerHTML);
                await browser.close();
                res.send(bodyHTML);
            });
        })();
    });
    

提交回复
热议问题