Ant*_*nos 86 javascript request amazon-web-services node.js aws-lambda
Node.js Alexa任务问题
我目前正在通过AWS Lambda编写Node.js Alexa任务,我一直在尝试编写一个函数,该函数从OpenWeather API接收信息并将其解析为一个名为的变量weather.相关代码如下:
var request = require('request');
var weather = "";
function isBadWeather(location) {
var endpoint = "http://api.openweathermap.org/data/2.5/weather?q=" + location + "&APPID=205283d9c9211b776d3580d5de5d6338";
var body = "";
request(endpoint, function (error, response, body) {
if (!error && response.statusCode == 200) {
body = JSON.parse(body);
weather = body.weather[0].id;
}
});
}
function testWeather()
{
setTimeout(function() {
if (weather >= 200 && weather < 800)
weather = true;
else
weather = false;
console.log(weather);
generateResponse(buildSpeechletResponse(weather, true), {});
}, 500);
}
Run Code Online (Sandbox Code Playgroud)
我在Cloud9和其他IDE中无数次运行了这个片段,它似乎运行得很完美.但是,当我将其压缩到一个包并将其上传到AWS Lambda时,我收到以下错误:
{
"errorMessage": "Cannot find module '/var/task/index'",
"errorType": "Error",
"stackTrace": [
"Function.Module._load (module.js:276:25)",
"Module.require (module.js:353:17)",
"require (internal/module.js:12:17)"
]
}
Run Code Online (Sandbox Code Playgroud)
我搜索了无数文章,并安装了模块-j,请求和许多其他Node模块,这些模块应该使这个代码运行,但似乎没有什么能解决这个问题.这是我的目录,以防万一:
- planyr.zip
- index.js
- node_modules
- package.json
Run Code Online (Sandbox Code Playgroud)
有谁知道这个问题是什么?非常感谢你提前.
Ant*_*nos 220
固定它!我的问题是我试图在Finder中使用Mac的内置压缩功能压缩文件.如果你是一个Mac用户,像我这样的,你应该在终端当你在你的项目的根目录(包含文件夹中运行以下脚本index.js,node_modules等文件).
zip -r ../yourfilename.zip *
Run Code Online (Sandbox Code Playgroud)
编辑:如果您是Windows用户,请在命令提示符(源)中运行以下命令.
Compress-Archive -LiteralPath node_modules, index.js -DestinationPath yourfilename.zip
Run Code Online (Sandbox Code Playgroud)
Ash*_*Jha 24
更新为已接受的答案:发生此错误时,表示您的zip文件不是AWS所需的有效格式.
如果双击zip,你会在你的代码文件中找到你的文件夹,但lambda想要当你双击zip时它会显示直接的代码文件.
为达到这个:
open terminal
cd your-lambda-folder
zip -r index.zip *
Run Code Online (Sandbox Code Playgroud)
然后,上传index.zip到AWS Lambda.
就我而言,这是因为我在内部 src 目录中有处理程序文件。
我必须从以下位置更改 Lambda 中的“处理程序”属性:
index.handler
Run Code Online (Sandbox Code Playgroud)
到
src/index.handler
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37574 次 |
| 最近记录: |