title: Calling puppeteer from rest api
body: <p>Is the appropriate way to call puppeteer from a rest endpoint?</p>
  
  <p>Route.js</p>
  
  <pre><code>const express = require('express');
  
  const PuppeteerController = require('../controllers/puppetter');
  
  const router = express.Router();
  
  router.post('/getPath', PuppeteerController.getPage);
  </code></pre>
  
  <hr>
  
  <p>Controller.js</p>
  
  <pre><code>async getPage(req, res) {
     try {
      let resp = await pageScanner.getPageContent(url);
      return res.status(200).send(resp);
     }
     catch(e) {
        return res.status(400).send({
          error: 'not-found'
        });
     }
  }
  </code></pre>
  
  <hr>
  
  <p>Scanner.js</p>
  
  <pre><code>async getPageContent(url) {
     try {
        const browser = await puppeteer.launch({
          args: [
            '--no-sandbox',
            '--disable-setuid-sandbox',
            '--disable-dev-shm-usage',
            '--disable-accelerated-2d-canvas',
            '--disable-gpu'
          ]
        });
        const page = await browser.newPage();
        await page.setViewport({ width: 800, height: 600 });
        await page.goto(url);
        await page.waitFor(1000);
        const pageContent = await page.content();
        await page.close();
        await browser.close();
        return pageContent;
     } catch(e) {
       return 'No good'
     }
  }
  </code></pre>
  
  <p>Is this the best way to do this? I'm concerned on performance in case I have multiple hits to this endpoint.</p>