1

我想在 AWS Lambda 函数上使用 Node.js 包 Chromeless。我使用两个包: chromless 和 serverless-chrome 在我的本地机器上,我的测试脚本运行良好。当我将它部署到 Lambda 时,我得到 .html() 的空结果结果不是空字符串,它是一个空页面。(<html><head></head><body></body></html>) CloudWatch 日志中没有错误。似乎 chrome 运行良好,但无法加载网站。NodeJS 版本是 8.10,async/await 似乎可以工作。希望有人有想法。

代码:

const launchChrome = require('@serverless-chrome/lambda');
const { Chromeless } = require('chromeless');

let index = async function handler () {

    await launchChrome({
        port: 9222,
        chromeFlags: [
            '--window-size=1200,800',
            '--disable-gpu',
            '--headless'
        ]
    })
    .then(async (chrome) =>
    {

        const chromeless = new Chromeless(
            {
                launchChrome:false,
                cdp:{host: 'localhost', port: 9222, secure: false}
            }
        );

        const html = await chromeless
            .goto('http://www.google.com')
            .wait(5000)
            .html();
        console.log(html);
        chromeless.end();

    })
};

exports.handler = index;

日志:

16:39:44 START RequestId: xxx Version: $LATEST
16:39:52 2018-04-05T16:39:52.163Z xxx   <html><head></head><body></body></html>
16:39:52 END RequestId: xxx
16:39:52 REPORT RequestId: xxx
Duration: 7375.83 ms    Billed Duration: 7400 ms Memory Size: 576 MB
4

1 回答 1

2

Chromeless 问题 [ https://github.com/graphcool/chromeless/issues/414]很好地记录了这个问题。

无服务器的依赖项中存在导致问题的问题。

要更正它,请将您的 serverless/package.json 更新为将“serverless-plugin-chrome”与版本“1.0.0-38”挂钩。

例如,我的开发依赖项如下所示:

    "devDependencies": {
    "@types/cuid": "^1.3.0",
    "@types/node": "^9.6.2",
    "serverless": "^1.19.0",
    "serverless-offline": "^3.15.3",
    "serverless-plugin-chrome": "1.0.0-38",
    "serverless-plugin-typescript": "^1.0.0"
  }
于 2018-04-07T02:00:50.683 回答