Skip to content

Commit

Permalink
Simplify sample, fix unit test (#746)
Browse files Browse the repository at this point in the history
  • Loading branch information
steren authored and fhinkel committed Oct 4, 2018
1 parent b81ec57 commit e8d83aa
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions appengine/headless-chrome/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,36 @@ const app = express();

let browser;

async function init () {
// [START browser]
browser = await puppeteer.launch({
args: ['--no-sandbox']
});
// [END browser]

const server = app.listen(process.env.PORT || 8080, async err => {
if (err) {
await browser.close();
return console.error(err);
}
const port = server.address().port;
console.info(`App listening on port ${port}`);
});
}

init();

app.use(async (req, res) => {
const url = req.query.url;

if (!url) {
return res.send('Please provide URL as GET parameter, for example: <a href="/?url=https://example.com">?url=https://example.com</a>');
}

if (!browser) {
// [START browser]
browser = await puppeteer.launch({
args: ['--no-sandbox']
});
// [END browser]
}

let page = await browser.newPage();
await page.goto(url);
const imageBuffer = await page.screenshot();

res.set('Content-Type', 'image/png');
res.send(imageBuffer);
});

const server = app.listen(process.env.PORT || 8080, err => {
if (err) {
return console.error(err);
}
const port = server.address().port;
console.info(`App listening on port ${port}`);
});
// [END full_sample]

module.exports = app;

0 comments on commit e8d83aa

Please sign in to comment.