AWS lambda:“模块'exports'上缺少Handler'handle'" [英] AWS lambda: "Handler 'handle' missing on module 'exports'"
问题描述
我已经将一个node js
快递项目上载到AWS lambda
.以下是我的处理程序代码另存为exports.js
:
I have uploaded a node js
express project to AWS lambda
. The following is my handler code saved as exports.js
:
const
express = require('express'),
bodyParser = require('body-parser'),
request = require('request'),
app = express().use(bodyParser.json()); // creates express http server
exports.handler = function(callback){
request('http://localhost/php-rest/api.php/routes?filter=route_short_name', function(error, response, body) {
if (!error && response.statusCode == 200) {
message = JSON.stringify(JSON.parse(body));
return callback(message, false);
} else {
return callback(null, error);;
}
});
}
app.get('/exports.handler', function(req, res) {
exports.handler(function(err, data){
if(err) return res.send(err);
res.send(data);
});
});
处理程序代码与我的app.js
文件是分开的.在AWS Lambda上进行测试时出现以下错误:
The handler code is separate from my app.js
file. I got the following error when I tested it on aws lambda:
{
"errorMessage": "Handler 'handler' missing on module 'exports'"
}
推荐答案
所以这是您的lambda函数,应该作为处理程序存在.在您的代码中,app.get()必须由AWS API Gateway处理.因为它是lambda函数的调用方法.您不能在lambda函数中使用nodejs服务器.
So this is your lambda function which should be there as a handler. In your code app.get() has to be handled by AWS API Gateway. because it is the method of invocation of lambda functions. You can't have the nodejs server inside the lambda function.
因此,.zip文件应命名为index.js,因为当我们上载.zip文件时,它将提取内容并找到我们提供的处理程序名称. 这些是应该上传的.zip文件内容.
So the .zip file should be named as index.js since when we upload the .zip file it extracts the contents and find the handler name which we have provided. These are the .zip file contents that should be uploaded.
-
index.js.zip
index.js.zip
-
node_modules
node_modules
index.js
package.json
package.json
package-lock.json
package-lock.json
这篇关于AWS lambda:“模块'exports'上缺少Handler'handle'"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!