dougmoscrop / serverless-http

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

Does not work correctly with inversify-express-utils #24

Closed Firaenix closed 5 years ago

Firaenix commented 7 years ago

Hi,

I was playing around with serverless-http and I love the idea of it! I did however, find an issue when trying to use inversify-express-utils.

Currently, using serverless-http the only way I can send data to my controllers is through using query strings. Even when using body-parser as middleware to express, when passing the body into the XHR that goes through serverless-http, it ends up getting to the controller as an empty object ({}).

If I run my server locally without using my configured lambda.ts, I am able to pass data via body to my controllers.

Any ideas on how to solve this issue?

Thanks very much!

Server.ts: import 'reflect-metadata'; import * as express from 'express'; import { json } from 'body-parser'; import { InversifyExpressServer } from 'inversify-express-utils'; import { InjectModule } from './Injector';

export class Server { public app: express.Application; private _server: InversifyExpressServer;

constructor() {
    this._server = new InversifyExpressServer(InjectModule);

    this.app = this._server.setConfig((app) => {
        app.use(json());
    }).build();

    this.app.listen(3000, () => {
        console.log('listening on port 3000');
    });
}

}

lambda.ts: import { Server } from './Server'; import * as serverless from 'serverless-http';

const server = new Server();

module.exports.handler = serverless(server.app);

dougmoscrop commented 7 years ago

Thanks for reporting this.

The way I expect to debug this would be to create a new integration test with your code and then see what's going on with debug. If it works locally then I'd move to arduous testing inside an actual Lambda. In this case I'd just write the code as ES6 JS not TS.

I will take a look when I find some time, but one thing I've noticed is the content-type/content-length header sometimes trips up body-parser.

dougmoscrop commented 7 years ago

Also since this is basically express, you can see how I have to set these headers:

https://github.com/dougmoscrop/serverless-http/blob/master/test/express.js#L53

I tried to work around the content-length being unset:

https://github.com/dougmoscrop/serverless-http/blob/master/lib/request.js#L33

It's possible this is hitting another scenario that I am missing or that something in an of these libraries has changed, or that something in APIg->Lambda changed :D

Firaenix commented 7 years ago

Thanks for getting back to me! If there is anything I can do to help, let me know!

I'm really keen on using your library so I'd love to have this working! 👍

dougmoscrop commented 7 years ago

Could you put together a minimal, non-TS version? For example, I don't know what is in ./Injector

Firaenix commented 7 years ago

Sure no worries, working on it now

Firaenix commented 7 years ago

Here you go, I've mocked up a very quick test server with a controller for you. I've got a readme in there if you need it 😄

TestInversifyServer.zip

dougmoscrop commented 6 years ago

I finally got around to peeking at this, sorry it only took over a year :)

I dunno, I wrote this test: https://github.com/dougmoscrop/serverless-http/blob/master/test/inversify.js

and it worked. I didn't obvious set up all the fancy controller/injection stuff. Can you take a look? See if I did something different?

dougmoscrop commented 5 years ago

I'm gonna close this out since there's at least a basic test, if anyone wants to PR a failing test I will take it and run with it