Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

req.path is incorrect when path ends with slash #391

Open
hedrall opened this issue Mar 19, 2021 · 3 comments
Open

req.path is incorrect when path ends with slash #391

hedrall opened this issue Mar 19, 2021 · 3 comments

Comments

@hedrall
Copy link

hedrall commented Mar 19, 2021

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
Copy link
Author

hedrall commented Mar 20, 2021

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

@codpot
Copy link
Contributor

codpot commented Sep 23, 2021

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

@lauterry
Copy link

lauterry commented Dec 5, 2023

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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants