0

I'm trying to figure out a way to start and stop a puppeteer scraper from an html onclick using NodeJS Express Mongodb Mongoose. Currently my method is to create a /start route and then start the async function for Puppeteer but still no idea on how I can stop it as I'm fairly new. Any other solution would be great. Thank you in advance.

My html Button

<input class="form-check-input" type="checkbox" id="startswitch" onclick='window.location.assign("/settings/start")'>

My Backend Router for puppeteer launch

router.get('/start', function(req, res) {
const puppeteer = require('puppeteer');
const UrL = "https://www.google.co.th";
(async () => {
    const browser = await puppeteer.launch({headless: false, args: ["--no-sandbox"]});
    //Some Scrapings Here
    //Some Scrapings Here
    //Some Scrapings Here
    //Some Scrapings Here
})();
});
Boost
  • 111
  • 1
  • 6
  • You can move the require and browser launch outside of the route. See [this](https://stackoverflow.com/questions/66935883/parallelism-of-puppeteer-with-express-router-node-js-how-to-pass-page-between-r/66946923#66946923) for an example. That said, what happens if there are multiple users? Do they each get their own browser or page? The spec doesn't seem completely clear. – ggorlen Aug 29 '21 at 14:08
  • Does this answer your question? [Parallelism of Puppeteer with Express Router Node JS. How to pass page between routes while maintaining concurrency](https://stackoverflow.com/questions/66935883/parallelism-of-puppeteer-with-express-router-node-js-how-to-pass-page-between-r) – ggorlen Aug 29 '21 at 14:08
  • I'm trying to make a website where users can toggle an html button to start or stop the webscraper which will be scraping data every minute while turned on from a crypto website and displaying the data in a table for the users to see. Only one browser of chrome will be needed and any user can start or stop that browser from the html toggle switch. I tried following the other post but still got lost because of the token management. – Boost Aug 29 '21 at 16:01

0 Answers0