Reputation: 1144
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);
});
});
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'"
}
Upvotes: 2
Views: 5065
Reputation: 498
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.
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
node_modules
index.js
package.json
package-lock.json
Upvotes: 8
Reputation: 27295
This normally appears if you don't have an index and start function. You can define it as index in the export hander:
exports.handler = function index(event, context, callback) {
// Your start code here
}
Upvotes: 3