serverless-http icon indicating copy to clipboard operation
serverless-http copied to clipboard

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

Open jimmythomson opened this issue 5 years ago • 4 comments

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.

jimmythomson avatar Sep 24 '20 16:09 jimmythomson

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

ninjazhai avatar Feb 07 '21 18:02 ninjazhai

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.

jimmythomson avatar Feb 07 '21 20:02 jimmythomson

A workaround with older versions was:

if(event.resource === "/"){
    event.path =  '/your-base-path';
}

traycho avatar Apr 17 '21 22:04 traycho

Solution

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

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

awsServerlessExpress.proxy(server, event, context);

}

brunofrota avatar Jun 20 '21 04:06 brunofrota