gpt4 book ai didi

node.js - 如何使用 NodeJS 在 AWS Lambda 上运行 PhantomJS

转载 作者:IT老高 更新时间:2023-10-28 22:03:44 29 4
gpt4 key购买 nike

在互联网上其他任何地方都没有找到有效的答案后,我提交了这个自问自答教程

如何在 AWS Lambda 上通过 NodeJS 脚本运行一个简单的 PhantomJS 进程?我的代码在本地机器上运行良好,但尝试在 Lambda 上运行时遇到了不同的问题。

最佳答案

编辑:这不再有效This is an apparent solution .


这里是一个简单的 PhantomJS 进程的完整代码示例,它作为 NodeJS child_process 启动。 It is also available on github .


index.js

var childProcess = require('child_process');
var path = require('path');

exports.handler = function(event, context) {

// Set the path as described here: https://aws.amazon.com/blogs/compute/running-executables-in-aws-lambda/
process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'];

// Set the path to the phantomjs binary
var phantomPath = path.join(__dirname, 'phantomjs_linux-x86_64');

// Arguments for the phantom script
var processArgs = [
path.join(__dirname, 'phantom-script.js'),
'my arg'
];

// Launc the child process
childProcess.execFile(phantomPath, processArgs, function(error, stdout, stderr) {
if (error) {
context.fail(error);
return;
}
if (stderr) {
context.fail(error);
return;
}
context.succeed(stdout);
});
}

幻影脚本.js

var system = require('system');
var args = system.args;

// Example of how to get arguments passed from node script
// args[0] would be this file's name: phantom-script.js
var unusedArg = args[1];

// Send some info node's childProcess' stdout
system.stdout.write('hello from phantom!')

phantom.exit();

要获得适用于亚马逊 Linux 机器的 PhantomJS 二进制文件,请访问 PhantomJS Bitbucket Page并下载 phantomjs-1.9.8-linux-x86_64.tar.bz2.

关于node.js - 如何使用 NodeJS 在 AWS Lambda 上运行 PhantomJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34645131/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com