68626

AWS lambda: “Handler 'handle' missing on module 'exports'”

Question:

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'" }

Answer1:

<a href="https://i.stack.imgur.com/vgSBl.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/vgSBl.png" data-original="https://i.stack.imgur.com/vgSBl.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a> 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.

<ul><li>

index.js.zip

<ul><li>

node_modules

</li> <li>

index.js

</li> <li>

package.json

</li> <li>

package-lock.json

</li> </ul></li> </ul>

Answer2:

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 }

Recommend

  • upload file with a form via ajax nodejs express
  • Jsoup Parsing: Parsing dynamic values as Key value pair
  • Why can't I start an Activity from a page Notification on Android Wear?
  • How to send email verification after user creation with Firebase Cloud functions?
  • why route of backbone needs the default route
  • Manage different base layouts in Angular2
  • React Router and Arbitrary Query Params: Page Refreshes Unintentionally on Load?
  • PWA with multiple pages
  • Using SWIG with a build system [closed]
  • force json_encode to create strings
  • node.js POST request fails
  • Google OAuth: can't get refresh token with authorization code
  • PHP Listener Script for Paypal Webhooks
  • What Makes These Two Array Adds Different?
  • Can't remove headers after they are sent
  • How to synchronize jQuery dialog box to act like alert() of Javascript
  • Authentication in Play! and RestEasy
  • Install PHP intl extension on MacOS
  • jQuery ready not fired after rails link_to is clicked
  • Magento Fatal error: Maximum execution error solution, on WAMP
  • How to attach a node.js readable stream to a Sendgrid email?
  • Bad request using file_get_contents for PUT request in PHP
  • Refering to the class itself from within a class mehod in Objective C
  • Get data from AJAX - How to
  • ImageMagick, replace semi-transparent white with opaque white
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Hazelcast - OperationTimeoutException
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • RestKit - RKRequestDelegate does not exist
  • Matrix multiplication with MKL
  • retrieve vertices with no linked edge in arangodb
  • A cron job substitute?
  • Revoking OAuth Access Token Results in 404 Not Found
  • json Serialization in asp
  • Linking SubReports Without LinkChild/LinkMaster
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?