moleculerjs / moleculer-web

:earth_africa: Official API Gateway service for Moleculer framework
http://moleculer.services/docs/moleculer-web.html
MIT License
291 stars 119 forks source link

404 when using fullPath in Aliases and CORS #269

Closed pibi closed 2 years ago

pibi commented 2 years ago

We are using moleculer-web 0.9.1 for an API service. We use a lot of custom APIs with fullPath and autoalias. We have noticed that the API-GW cors code doesn't match the route when the path doesn't start with the routes path. In this case the cors preflight doesn't work, it returns a 404.

To show you the issue, we have managed to reproduce it in the API-GW by using the aliases:

        routes: [
            {
                path: "/api",

                aliases: {
                    "welcome": {
                        method: "POST",
                        fullPath: "/welcome",
                        action: "greeters.welcome"
                    }
                },
            }
        ],              
[2021-08-30T16:57:58.361Z] INFO  spectrex360-34464/API: Register route to '/api'
[2021-08-30T16:57:58.362Z] INFO  spectrex360-34464/API:     POST /welcome => greeters.welcome
[2021-08-30T16:58:12.621Z] DEBUG spectrex360-34464/API: undefined
[2021-08-30T16:58:12.622Z] INFO  spectrex360-34464/API: <= 404 OPTIONS /welcome [+1.032 ms]

if the fullPath starts with the route path, the cors is correctly set:

                aliases: {
                    "welcome": {
                        method: "POST",
                        fullPath: "/api/welcome",
                        action: "greeters.welcome"
                    }
                },
[2021-08-30T16:56:46.501Z] INFO  spectrex360-34464/API: Register route to '/api'
[2021-08-30T16:56:46.505Z] INFO  spectrex360-34464/API:     POST /api/welcome => greeters.welcome
[2021-08-30T16:57:21.114Z] INFO  spectrex360-34464/API: => OPTIONS /welcome
[2021-08-30T16:57:21.115Z] INFO  spectrex360-34464/API: <= 204 OPTIONS /api/welcome [+1.492 ms]
intech commented 2 years ago

Confirmed on version 0.10.0

icebob commented 2 years ago

The problem is that, there is no registered handler for the path with OPTIONS method. Here is a workaround: image

icebob commented 2 years ago

It can solve it, but I don't know it doesn't cause other issues...e.g It can find a GET alias and not the POST image

icebob commented 2 years ago

Fixed.