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

req.path is incorrect when path ends with slash

Open hedrall opened this issue 4 years ago • 3 comments

req.path is set like below.

1. https://domain/test => req.path = '/test'
2. https://domain/test/ => req.path = '/test'

2 is not correct, since Express actually set '/test/' to path in this case.

I think the cause is that the req.path is obtained from event.pathparameters.proxy instead of event.path, and the last slash is not included in event.pathparameters.proxy.

hedrall avatar Mar 19 '21 00:03 hedrall

What is the purpose of giving priority to the pathparameters setting over the path?

hedrall avatar Mar 20 '21 02:03 hedrall

I'm having same issue.

This problem cause because event.pathparameters don't have trailing slash which event from AWS.

You can try 2 ways temporary to fix this issue.

(1) Use Version 4.3.10

    "@vendia/serverless-express": "4.3.10"

Related #362, #397, #400, #441, #454, #455

In version 4.3.10, event.path is used which is having trailing slash correctly.

But, aware that event.pathParameters.proxy returning value of {proxy+}, but event.path returning full path.

(2) Change {proxy+} to {somethingelse+} with (version > 4.0.0 && version != 4.3.10)

// Part of serverless.yml
      - http:
          path: '{any+}'
          method: any

With above configuration,

https://github.com/vendia/serverless-express/blob/6adac653fe1daf1dd44ff6aa17dbf08f07662173/src/event-sources/utils.js#L7

event.pathParameters.proxy goes to undefined, and event.pathParameters.any having some values.

So, that line path have value of event.path which same as Version 4.3.10.

Also, aware that event.path containing full path.

Might you can have trouble when migrating from {proxy+} to {any+} with using serverless framework.

An error occurred: ApiGatewayResourceProxyVar - Resource handler returned message: "A sibling ({any+}) of this resource already has a variable path part -- only one is allowed (Service: ApiGateway, Status Code: 400, Request ID: 143ad1b3-bfd4-4fd5-a320-aa3a8cb406a4, Extended Request ID: null)" (RequestToken: 61541bf7-b149-a9f6-16d2-1333e6eccd74, HandlerErrorCode: InvalidRequest).

So, I recommend to undeploy and deploy again as easiest way. (This way may removing AWS Cloud Watch logs, and have some downtimes.)

See Also, serverless/serverless#3785

codpot avatar Sep 23 '21 10:09 codpot

Hello

This issue prevents me from detecting the trailing slash because req.path return https://domain/test/ => '/test' instead of /test/

Is it possible de make req.path return the trailing slash please ?

lauterry avatar Dec 05 '23 15:12 lauterry