Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS lambda: "Handler 'handle' missing on module 'exports'"

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'"
} 
like image 732
Rashik Avatar asked Sep 28 '26 13:09

Rashik


1 Answers

enter image description here 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

like image 117
SamPiy93 Avatar answered Oct 01 '26 03:10

SamPiy93



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!