dougmoscrop / serverless-http

Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda 🎉
Other
1.74k stars 167 forks source link

I can't seem to get non-null result! Koa and serverless #125

Closed EralpB closed 7 months ago

EralpB commented 5 years ago

Hello my lambda always gives null as output, what could I be doing wrong? exports, imports, promise handling etc

MainServer.js:

const Koa = require('koa');
const Router = require('koa-router');
const StaticServer = require('koa-static-server');
const BodyParser = require('koa-bodyparser');
const SSLify = require('koa-sslify');
const Ejs = require('koa-ejs');
const Config = require("Server/Config");
const PageRoutes_1 = require("Server/PageRoutes");
const app = new Koa();
exports.router = new Router();
app.use(BodyParser());
app.use(StaticServer({ rootDir: 'assets', rootPath: '/assets', last: true }));
app.use(StaticServer({ rootDir: 'dist/statics', rootPath: '/statics', last: true }));
Ejs(app, ({
    root: path.join(__dirname, '../ejs'),
    cache: Config.IS_PRODUCTION,
    layout: 'template',
    viewExt: 'ejs',
    debug: true,
}));
PageRoutes_1.default(exports.router);
app.use(exports.router.routes());
app.use(exports.router.allowedMethods());
module.exports.handler = app;

and my index.js:

const serverless = require('serverless-http');
const app = require('MainServer');
const my_handler = serverless(app);

module.exports.handler = function(event, context, callback) {
    const response = my_handler(event, context);
    response.then(
        (data) => {
            console.log('promise resolved');
            console.log(data);
            callback(null, data);
        },
        (err) => {
            console.log('ERROR');
            console.log(err);
            callback(err);
        }
    );

}

this is one of the things I tried I also tried: I can't even see the logs

module.exports.handler = async (event, context) => {
  console.log('Im here')
  try {
        const result = await my_handler(event, context);
        console.log('Here33');
        console.log(result)
        return result;
    }
    catch (err) {
        console.log('Here4')
        console.log(err);
        return err;
    }
}

I know the handler function is correct I put a dummy JSON and it correctly returns.

https://stackoverflow.com/questions/58076375/lambda-and-koa-server-promise-handling

dougmoscrop commented 5 years ago

Are you using latest version of serverless-http?

What is my_handler? From your code, MainServer exposes a .handler but you are importing it directly?