AWS Lambda执行dynamic代码
是否有可能定义一个NodeJs函数来评估dynamicnodejs代码?
这里是上下文:
用户创build自定义的JavaScript函数应该返回true / false。
我需要“评估”在NodeJ上运行的AWS Lambda容器中的用户代码。
可能吗?
我应该使用类似于JavaScript eval
函数的东西吗?
编辑
这是我的尝试
'use strict'; exports.handler = (event, context, callback) => { var body = "function test() { return 10; };"; console.log("body", body); eval(body); var result = test(); callback(null, result); };
我得到一个错误,说“testing”没有定义,因此eval
不正确。
START RequestId: 6e9abd93-bd69-11e7-a43f-c75328d778e1 Version: $LATEST 2017-10-30T11:56:58.569Z 6e9abd93-bd69-11e7-a43f-c75328d778e1 body function test() { return 10; }; 2017-10-30T11:56:58.581Z 6e9abd93-bd69-11e7-a43f-c75328d778e1 ReferenceError: test is not defined at exports.handler (/var/task/index.js:11:18) END RequestId: 6e9abd93-bd69-11e7-a43f-c75328d778e1 REPORT RequestId: 6e9abd93-bd69-11e7-a43f-c75328d778e1 Duration: 32.78 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 19 MB RequestId: 6e9abd93-bd69-11e7-a43f-c75328d778e1 Process exited before completing request
eval在Lambda中工作正常。 删除“严格使用” ,它会正常工作,输出10。
严格模式不允许创build全局variables,这就是为什么你得到错误。
第二个选项是明确地将该函数添加到全局上下文中:
'use strict'; exports.handler = (event, context, callback) => { var body = "global.test = function() { return 10; };"; console.log("body", body); eval(body); var result = test(); callback(null, result); };
节点只是在服务器端执行javascript的运行时。您可以在节点(Lambda)中编写任何有效的Javascript代码。
正如我在代码中看到'eval'不执行,你可以请尝试安装包'npm install eval' – https://www.npmjs.com/package/eval
- 将私钥存储在静态的Angular站点上
- 无法使用电子邮件创buildGoogle now卡
- 我如何在亚马逊ec2服务器上find我的Nameservers?
- AWSfile upload超过5MB – nodejs
- 消息AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool不是aws lambda中的函数
- AWS中的lambda函数返回后,variables为null
- Amazon S3 getObject桶策略
- 什么可能会阻止EC2托pipe的机器人在Microsoft Bot Framework上回复消息?
- 如何在Node.js中从AWS getObject调用中返回variables