J
J
jestkI2021-04-03 22:38:31
Express.js
jestkI, 2021-04-03 22:38:31

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

app.post('/api/auth/check', async (req, res) => {
  try {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(
    'https://www.google.com'
  );
  res.json({message: 'Success'})
  } catch (e) {
  console.log(e);
  res.status(500).json({ message: 'Error' });
  }});


  app.post('/api/auth/register', async (req, res) => {
  console.log('register');
  // Сюда нужно передать текущую сессию пользователя (page и browser) и далее выполнять действия на той-же странице.
  await page.waitForTimeout(1000);
  await browser.close();
  }});


Is it possible to somehow pass page and browser from one route to another while maintaining puppeteer concurrency? If you set a global variable, then page and browser will be overwritten and multitasking will not work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
olmax04, 2021-04-04
@jestkI

Everything turned out to be easier. Made a binding to the session by creating a hash.
var createHash = require('hash-generator');
let session = createHash(12);
And then I set the variables in the post requests themselves, which will be used in the routes.
//set variable
app.set(`login_${session}`, user_login);
//call it in other routes
app.get(`login_${session}`);
I pass the session variable through react to each route, and thus everything works

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question