-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from 21 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
a6b9c28
puppeteer sample (Headless Chrome)
steren f40b442
Fix snippet name
steren 9708edc
newline for app.yaml
steren ecf1f20
update test to use `server` instead of `app`
steren 6c05d34
Use app.js to fix cloud-repo-tools error
steren 9cae70e
Add test for screenshot
steren 8638c40
Update browser snippet
steren d478a66
Remove unecessary `()`
steren 1d00f38
Update README
steren a610c3f
Fix typo
steren 4fc6f91
Merge branch 'master' into master
steren 833f322
Merge branch 'master' of github.com:steren/nodejs-docs-samples
steren cf566d6
Remove unecessary `()`
steren 2a584ed
Use app.standard.yaml
steren e7616bb
Update @google-cloud/nodejs-repo-tools dep
steren dcc642e
point to GAE instructions for local run and deployment
steren c80a5f2
Merge branch 'master' into master
steren f4a2616
merge
steren e102d5a
Headless Chrome: start browser outside of user's request.
steren 41f0f51
clean up
steren 86f7e56
move init up
steren fb85be2
Merge branch 'master' into master
steren 9cb2e19
Merge branch 'master' into master
steren cef1907
more async await
steren c4e5839
Store the browser as global variable, not the page
steren 6b29d26
Merge branch 'master' into master
steren 0bd56a9
remove trailing whitespace
steren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,32 +20,40 @@ const express = require('express'); | |
const puppeteer = require('puppeteer'); | ||
const app = express(); | ||
|
||
let page; | ||
|
||
async function init () { | ||
// [START browser] | ||
const browser = await puppeteer.launch({ | ||
args: ['--no-sandbox'] | ||
}); | ||
// [END browser] | ||
page = await browser.newPage(); | ||
const server = app.listen(process.env.PORT || 8080, err => { | ||
if (err) { | ||
browser.close(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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(); | ||
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; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
async error => {