AWS Lambda:模块初始化错误:TypeError

时间:2017-02-02 01:18:43

标签: node.js express aws-lambda

我正在尝试部署AWS lambda函数,我已经用express编写了代码:

代码:

 var express = require('express');
    var bodyParser = require('body-parser');
    var lampress = require('lampress');
    var request = require('request');
    var app = express();

    app.set('port', (process.env.PORT || 5000));

    // Process application/x-www-form-urlencoded
    app.use(bodyParser.urlencoded({extended: false}));

    // Process application/json
    app.use(bodyParser.json());

    // Index route
    app.get('/', function (req, res) {
        res.send('Hello! I am a Chatbot designed to help you learn  Type "begin" to start a chat! You can type "begin" at any time to return to the first menu');
    });

    // for Facebook verification
    app.get('/webhook/', function (req, res) {
        if (req.query['hub.verify_token'] === 'xyz') {
            res.send(req.query['hub.challenge']);
        }
        res.send('Error, wrong token');
    });

    // Spin up the server
     app.listen(app.get('port'), function() {
        console.log('running on port', app.get('port'));
    });

    //figure out if your greeting text is working
    //need persistent menu?
    app.post('/webhook/', function (req, res) {
        getStarted();
        greetingText();
        messaging_events = req.body.entry[0].messaging;
        for (i = 0; i < messaging_events.length; i++) {

            event = req.body.entry[0].messaging[i];
            sender = event.sender.id;
            if (event.message && event.message.text) {
            //code
            }
            if (event.postback) {
            //code
            }
            console.log('********2');
        }
        res.sendStatus(200)
    });

    exports.handler = lampress(app, function() {
        console.log("Server has started");
    });

错误:

 module initialization error: TypeError
        at lampress (/var/task/node_modules/lampress/index.js:82:10)
        at Object.<anonymous> (/var/task/index.js:829:23)
        at Module._compile (module.js:409:26)
        at Object.Module._extensions..js (module.js:416:10)
        at Module.load (module.js:343:32)
        at Function.Module._load (module.js:300:12)
        at Module.require (module.js:353:17)
        at require (internal/module.js:12:17)*

我有适当的node_modules。为什么不这样做呢。

压缩拉链结构     - &GT; index.js     - &GT; node_modules文件夹。

的package.json: &#34; lampress&#34;:&#34; ^ 1.1.1&#34;

3 个答案:

答案 0 :(得分:1)

根据您采购模块的方式,您可以尝试:

require('./lampress');

此外,在您的路线处理程序中,进行小修正以避免两次发送响应:

app.get('/webhook/', function (req, res) {
    if (req.query['hub.verify_token'] === 'xyz') {
        return res.send(req.query['hub.challenge']);
    }
    res.send('Error, wrong token');
});

答案 1 :(得分:1)

我认为您的问题位于exports.handler = lampress(...,因为lampress docs lampress()需要2个参数,第一个是端口号,第二个是服务器。你已经为第一个参数传递了一个服务器,为第二个参数传递了一个函数,因此lampress会抛出一个TypeError

正确的代码是:

exports.handler = lampress(<your port number>, app);

答案 2 :(得分:0)

仅供参考 对我而言,是source-map-support放弃了实际的错误消息和堆栈跟踪。 一旦我注释掉source-map-support导入行,该错误就开始显示实际的错误消息和堆栈跟踪。 https://github.com/evanw/node-source-map-support/issues/240