dougmoscrop / serverless-http

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

404 returned when requesting '/' when using `basePath` #181

Open jimmythomson opened 3 years ago

jimmythomson commented 3 years ago

If I try and GET /my-base-path on an app where basePath is set to /my-base-path, the underlying event.path appears to be set to a blank string, which then results in express responding with a 404 with the message Cannot GET null. Could someone confirm that this is indeed a bug rather than me not implementing/using something correctly? Happy to create a PR to resolve this if indeed it is an issue. Thanks.

ninjazhai commented 3 years ago

I've encountered this issue. Kinda similar to the one reported here. Were you able to find a solution?

jimmythomson commented 3 years ago

Hi @ninjazhai - I just had a look back at some code that I was writing when I ran in to this problem, and it looks like I just had to create a route where the path was a blank string, i.e. app.get('', myHandler) rather than a single forward slash. Hope that helps you.

traycho commented 3 years ago

A workaround with older versions was:

if(event.resource === "/"){
    event.path =  '/your-base-path';
}
brunofrota commented 3 years ago

Solution

exports.handler = (event, context) => {

    if (event.pathParameters ==null )  {
        event.path = '/';
    }
    else{
        event.path = '/'+event.pathParameters.proxy;
    }

awsServerlessExpress.proxy(server, event, context);

}