Parallelism of Puppeteer with Express Router Node JS. How to pass page between routes while maintaining concurrency

2 min read 05-10-2024
Parallelism of Puppeteer with Express Router Node JS. How to pass page between routes while maintaining concurrency


Conquering Concurrency: Seamlessly Sharing Puppeteer Pages Between Express Routes

The Challenge:

You're building a Node.js application using Express, and you want to leverage the power of Puppeteer to interact with web pages. But you also need to handle multiple requests concurrently, making it tricky to share Puppeteer pages across your routes without sacrificing performance.

Let's break it down:

Imagine a scenario where your application needs to fetch data from multiple websites. Using Puppeteer, you can create a browser instance and launch pages to scrape the data. However, if you create a new page for every request, you'll end up with many browser instances, unnecessarily consuming resources. Sharing a single page across requests seems like the solution, but how do you maintain concurrency without blocking other requests?

Enter the Solution:

The key to this puzzle lies in understanding Puppeteer's asynchronous nature and leveraging Express's middleware capabilities.

Original Code (Illustrative Example):

const express = require('express');
const puppeteer = require('puppeteer');

const app = express();
const port = 3000;

app.get('/scrape1', async (req, res) => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  // ... scrape data from website1 ...
  res.send(data);
  await browser.close();
});

app.get('/scrape2', async (req, res) => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  // ... scrape data from website2 ...
  res.send(data);
  await browser.close();
});

app.listen(port, () => {
  console.log(`Server listening on port ${port}`);
});

This code creates a new browser and page for every request, leading to unnecessary resource consumption.

The Improved Approach:

  1. Centralized Browser: Instead of launching a new browser for each request, we'll create a single browser instance accessible to all routes.
  2. Middleware for Page Sharing: We'll use Express middleware to manage page sharing and ensure that only one request uses a specific page at a time.
const express = require('express');
const puppeteer = require('puppeteer');

const app = express();
const port = 3000;

let browser;
let sharedPage;

// Middleware for page sharing
app.use(async (req, res, next) => {
  if (!browser) {
    browser = await puppeteer.launch();
    sharedPage = await browser.newPage();
  }

  // Ensure only one request uses the shared page at a time
  req.page = sharedPage;

  next();
});

app.get('/scrape1', async (req, res) => {
  const { page } = req; 
  // ... scrape data from website1 using page ...
  res.send(data);
});

app.get('/scrape2', async (req, res) => {
  const { page } = req; 
  // ... scrape data from website2 using page ...
  res.send(data);
});

app.listen(port, () => {
  console.log(`Server listening on port ${port}`);
});

Explanation:

  • The middleware ensures that only one request uses the sharedPage at a time, allowing for efficient page sharing.
  • The req.page property allows each route to access the shared page without any modifications.
  • This approach enables concurrency, allowing multiple requests to be processed simultaneously while sharing the same page, thus minimizing resource consumption.

Key Considerations:

  • Resource Management: While this approach offers concurrency, it's crucial to manage browser and page lifecycles effectively. Consider closing the browser and pages after a certain period of inactivity or when they are no longer required.
  • Memory Leaks: Ensure proper resource management to prevent memory leaks, especially if you're dealing with complex web pages or long-running processes.
  • Error Handling: Implement robust error handling mechanisms to address potential errors during page navigation, data extraction, or network issues.

By effectively managing page sharing and concurrency, you can unlock the true power of Puppeteer in your Express applications, creating faster, more efficient, and scalable solutions.

References: