Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Headless Chrome: keep browser alive between requests. #675

Merged
merged 27 commits into from
Sep 12, 2018
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a6b9c28
puppeteer sample (Headless Chrome)
steren May 25, 2018
f40b442
Fix snippet name
steren May 25, 2018
9708edc
newline for app.yaml
steren May 25, 2018
ecf1f20
update test to use `server` instead of `app`
steren May 29, 2018
6c05d34
Use app.js to fix cloud-repo-tools error
steren May 29, 2018
9cae70e
Add test for screenshot
steren Jun 2, 2018
8638c40
Update browser snippet
steren Jun 2, 2018
d478a66
Remove unecessary `()`
steren Jun 5, 2018
1d00f38
Update README
steren Jun 5, 2018
a610c3f
Fix typo
steren Jun 5, 2018
4fc6f91
Merge branch 'master' into master
steren Jun 5, 2018
833f322
Merge branch 'master' of github.com:steren/nodejs-docs-samples
steren Jun 5, 2018
cf566d6
Remove unecessary `()`
steren Jun 5, 2018
2a584ed
Use app.standard.yaml
steren Jun 5, 2018
e7616bb
Update @google-cloud/nodejs-repo-tools dep
steren Jun 5, 2018
dcc642e
point to GAE instructions for local run and deployment
steren Jun 5, 2018
c80a5f2
Merge branch 'master' into master
steren Jun 5, 2018
f4a2616
merge
steren Jun 24, 2018
e102d5a
Headless Chrome: start browser outside of user's request.
steren Jun 24, 2018
41f0f51
clean up
steren Jun 24, 2018
86f7e56
move init up
steren Jun 25, 2018
fb85be2
Merge branch 'master' into master
steren Jun 27, 2018
9cb2e19
Merge branch 'master' into master
steren Aug 5, 2018
cef1907
more async await
steren Aug 9, 2018
c4e5839
Store the browser as global variable, not the page
steren Aug 30, 2018
6b29d26
Merge branch 'master' into master
steren Sep 11, 2018
0bd56a9
remove trailing whitespace
steren Sep 12, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions appengine/headless-chrome/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,41 @@ const express = require('express');
const puppeteer = require('puppeteer');
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>');
}

// [START browser]
const browser = await puppeteer.launch({
args: ['--no-sandbox']
});
// [END browser]
const page = await browser.newPage();
let page = await browser.newPage();
await page.goto(url);
const imageBuffer = await page.screenshot();
browser.close();

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;