admin管理员组

文章数量:1302406

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

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'"
} 
Share Improve this question edited Aug 8, 2018 at 20:50 Nisarg 1,6417 gold badges20 silver badges31 bronze badges asked Aug 8, 2018 at 20:47 RashikRashik 1,1542 gold badges16 silver badges36 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

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

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
}

本文标签: javascriptAWS lambda quotHandler 39handle39 missing on module 39exports39quotStack Overflow