I am currently working on project where I need to build an application that needs to open an URL in a browser in order to use some functions on it.
for that I used puppeteer inside a nodejs script in order to open the browser on the server side so I can use it like an api .
Here's the code (nodejs):
app.get('/do', (req, res) => {
console.log("ok");
(async() => {
var browser = await puppeteer.launch(
{ args: ['--no-sandbox','--disable-setuid-sandbox'], headless: false });
var page = await browser.newPage();
await page.goto('https://url.com');//i hid the url for personal reason
await page.waitFor(1000); // to wait for 1000ms
await page.waitFor('body div'); // to wait for the 'body div' selector in body
await page.waitFor(() => Math.random() < 0.5); // to wait for the predicate
await page.screenshot({
path: 'public/photo.png'
});
await browser.close();
await res.end('<html><head></title></head><body><h1><img src=photo.png ></img></h1></body></html>');
})();
});
This code works locally but when I deploy on heroku it shows me this error :
app[web.1]: /send
app[web.1]: (node:4)
UnhandledPromiseRejectionWarning: Unhandled promise rejection
(rejection id: 1): Error: Failed to launch chrome!
app[web.1]:
/app/node_modules/puppeteer/.local-chromium/linux-515411/chrome-linux/nacl_helper:
error while loading shared libraries: libnss3.so: cannot open shared
object file: No such file or directory
app[web.1]:
[21:21:1228/131735.202176:ERROR:nacl_fork_delegate_linux.cc(316)] Bad
NaCl helper startup ack (0 bytes)
app[web.1]:
app[web.1]:
app[web.1]: TROUBLESHOOTING:
https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md
app[web.1]:
app[web.1]: (node:4) [DEP0018]
DeprecationWarning: Unhandled promise rejections are deprecated. In
the future, promise rejections that are not handled will terminate the
Node.js process with a non-zero exit code.
But if I remove headless: false
it works but the problem is that the url shows a warming page that I need to use a browser like chrome or mozilla or safari.
How can I resolve this issue ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…