I am using nodejs/puppeeteer and a route for a small api. I am simply trying to see when the puppeteer scripts are done. When its done, I want to show something in my HTML page. Not sure what i need to modify on the route. I just know I need to return the data when done and somehow check on the html page?
const express = require('express')
const puppeteer = require('puppeteer');
const bodyParser = require('body-parser');
const app = express()
const port = 1000
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static('public'));
const goToSite = async(url) => {
const browser = await puppeteer.launch({
headless: false
});
const page = await browser.newPage();
await page.waitFor(1000);
};
app.get('/', (req, res) => {
res.sendFile(__dirname '/index.html');
})
app.post('/puppeteer', (req, res) => {
goToSite(req.body.url);
})
CodePudding user response:
Just use the response object res
to send something once done:
const goToSite = async (url, res) => {
const browser = await puppeteer.launch({
headless: false
});
const page = await browser.newPage();
await page.waitFor(1000);
page.evaluate(() => {
const div = document.createElement('div');
div.innerHTML = 'done';
document.body.appendChild(div);
});
const html = await page.content();
res.send(html);
res.end();
};
app.post('/puppeteer', (req, res) => {
goToSite(req.body.url, res);
});