Express stops listening when using Puppeteer

Xaccer

New member
#1
I'm currently writing a simple API which is performing actions via Puppeteer, however when trying to execute my script so I can access the API; my Express app seems to stop listening once Puppeteer opens?

Here's my script:
Code:
const Apify = require('apify');
const puppeteer = require('puppeteer-extra');
const RecaptchaPlugin = require('puppeteer-extra-plugin-recaptcha');

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

let page;
let browser;
let device_imei;

puppeteer.use(
    RecaptchaPlugin({
        provider: {
            id: '2captcha',
            token: '2captcha-token'
        },
        visualFeedback: true
    })
);

const handle_response = async (response) => {
    const url = response.url();
    try {
        const req = response.request();
        const orig = req.url();
        let status;
        let text;
        if(response.status) {
            status = response.status();
        }
        if(
            status
            && !(status > 299 && status < 400)
            && !(status === 204)
            && (req.resourceType() === 'xhr')
            && (orig.startsWith('https://www.website.com/apis'))
        ) {
            text = await response.text();
            if(text.includes('Access Denied')) {
                console.log('Access Denied, we need a new IP');
                return;
            }
            const json = JSON.parse(text);
            if(json['orderFlowResponseDO']['validationErrors'] !== undefined) {
                console.log({orig, status, text: json});
            }
        }
    } catch (err) {
        console.error(`Failed getting data from: ${url}`);
        console.error(err);
    }
};

const launch_browser = async () => {
    Apify.main(async () => {
        browser = false;
        browser = await Apify.launchPuppeteer({
            args: [
                '--no-sandbox',
                '--proxy-server=' + await get_proxy()
            ],
            headless: true,
            stealth: true,
            stealthOptions: {
                hideWebDriver: true
            }
        });
        page = await browser.newPage();
        page.on('response', handle_response);
        browser.on( 'disconnected', launch_browser);
    });
};

const get_proxy = async () =>
{
    return '127.0.0.1:80'; //example
};

(async () => {
    await launch_browser();
})();

app.get('/submit/:imei', async function (req, res) {
    res.setHeader('Content-Type', 'application/json');
    res.end(JSON.stringify({
        'test': 'testvalue'
    }));
});

app.listen(3000);
When I run node index.js I get the following output:
Code:
curtis@Curtiss-MacBook-Pro-2 untitled3 % node index.js
INFO  System info {"apifyVersion":"0.21.0","apifyClientVersion":"0.6.0","osType":"Darwin","nodeVersion":"v13.0.1"}
WARN  Neither APIFY_LOCAL_STORAGE_DIR nor APIFY_TOKEN environment variable is set, defaulting to APIFY_LOCAL_STORAGE_DIR="/Users/curtis/Downloads/untitled3/apify_storage"
INFO  Launching Puppeteer {"args":["--no-sandbox","--proxy-server=127.0.0.1:80","--no-sandbox","--user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"],"headless":true,"stealth":true,"defaultViewport":{"width":1366,"height":768}}
curtis@Curtiss-MacBook-Pro-2 untitled3 %