4

I am trying to deploy AWS lambda function and I have written code in express:

Code:

 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");
    });

Error:

 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)*

I have proper node_modules in place. Why wont this work.

compressed zip structure --> index.js --> node_modules folder.

package.json: "lampress": "^1.1.1"

3 Answers 3

2

FYI For me, it was source-map-support that discarded the actual error message and stack trace. Once I commented out the source-map-support import line then the error started showing the actual error message and stack trace. https://github.com/evanw/node-source-map-support/issues/240

Sign up to request clarification or add additional context in comments.

Comments

1

I think your problem is at exports.handler = lampress(... as per the lampress docs lampress() takes 2 arguments, the first a port number and the second a server. You've passed in a server for the first argument and a function for the second, so lampress throws a TypeError

The correct code would be:

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

6 Comments

Thats for version 2.0.0. Installing lampress via npm install lampress installs 1.1.1 so the docs at npmjs.com/package/lampress should be correct.
He didn't mention how he installed it. And more significantly, the stack trace of his error seems to be originating from a require statement.
I have installed version npm -v lampress 4.1.2 . I am not sure if I am in the right version packge.json has : "lampress": "^1.1.1" should
@suchu34 What would be the port name while deploying in AWS lambda: Heroku and openshift has : envirnment object which is process.env
I have created new function with the mentioned code format and I get the error: stackoverflow.com/questions/42008247/…
|
1

Depending on how you sourced the module, can you try:

require('./lampress');

Also, in your route handler, minor correction to avoid sending a response twice:

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');
});

3 Comments

Thanks for the correction Sylon.changing require('./lampress'); I am facing module initialization error. Since It cannot find lampress node module.
Exact error: Error: Cannot find module './lampress' at Function.Module._resolveFilename (module.js:469:15) at Function.Module._load (module.js:417:25)
I have created new function and I get the error: stackoverflow.com/questions/42008247/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.