我在水滴中数字海洋 https://www.digitalocean.com/我收到这个错误。
(node:5549) UnhandledPromiseRejectionWarning: TimeoutError: Navigation Timeout Exceeded: 300000ms exceeded
at Promise.then (/var/www/screenshot/node_modules/puppeteer/lib/NavigatorWatcher.js:94:
at <anonymous>
我试图截图的网址是https://www.pontofrio.com.br/ https://www.pontofrio.com.br/
我添加了一个用户代理来绕过针对无头请求的保护。
它在我的本地机器上运行但是当我在 VPS 上运行时,即使我增加了超时值,它也会出现错误。
我正在和邮递员一起测试。
本地计算机 Windows 8.1 x64 - 工作
虚拟机 Linux 16.04 x64 - 工作
VPS Linux 16.04 x64 - 失败
这是代码
const bodyParser = require('body-parser');
const cors = require('cors');
const express = require('express');
const fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');
const puppeteer = require('puppeteer');
const PORT = 5005;
const userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3542.0 Safari/537.36';
express()
.use(cors())
// parse application/x-www-form-urlencoded
.use(bodyParser.urlencoded({ extended: true }))
// parse application/json
.use(bodyParser.json())
.post('/screenshot', (req, res) => {
let result;
const filename = req.body.filename;
const filepath = path.join(__dirname, 'images');
const url = req.body.url;
const wdt = req.body.width || 1366;
const hgt = req.body.height || 768;
if (!fs.existsSync(filepath)) mkdirp.sync(filepath);
(async () => {
const browser = await puppeteer.launch({
executablePath: '/opt/google/chrome/chrome',
args: [
'--disable-setuid-sandbox',
'--disable-gpu',
'--no-first-run',
'--no-sandbox',
]
});
const page = await browser.newPage();
await page.setUserAgent(userAgent);
await page.goto(url, { waitUntil: 'networkidle2', timeout: 300000 });
await page.setViewport({ width: parseInt(wdt), height: parseInt(hgt) });
await page.screenshot({ path: `${filepath}/${filename}.jpg` });
await browser.close();
if (fs.existsSync(path.join(`${filepath}/${filename}.jpg`))) {
result = `Imagem salva em: ${filepath}/${filename}.jpg`;
} else {
result = 'Erro ao salvar imagem, tente novamente.';
}
res.send(result);
})();
})
.use('/screenshot', express.static((path.join(__dirname, '/images'))))
.listen(PORT, () => console.log(`Rodando na porta ${PORT}`));