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

Unable to use proxyRequest in router eventHandler #257

Closed
chrisnoden opened this issue Nov 30, 2022 · 5 comments
Closed

Unable to use proxyRequest in router eventHandler #257

chrisnoden opened this issue Nov 30, 2022 · 5 comments

Comments

@chrisnoden
Copy link

chrisnoden commented Nov 30, 2022

Environment

"h3": "^1.0.1",
"nuxt": "^3.0.0",

Reproduction

Probably not a bug but a problem with my implementation, but help would be appreciated.

I am using h3 to create a proxy server - to send some /api requests to a back-end server. The API server is not publicly accessible.

I've created a router (createRouter()) and am adding routes via router.use()

It works fine but the body of a POST request is dropped. I've been using sendProxy() and saw that proxyRequest() is recommended to ensure headers and POST body are sent on to the back-end server.

However, within the eventHandler() returning a proxyRequest() results in none of my /api requests working.

eg

router.use(
    '/api/foo/**',
    eventHandler((event) => {
        if (isRequestAuthenticated(event)) {
            return proxyRequest(
                event,
                'https://backend.example.com/api/bar'
            )
        }
    })
)

Describe the bug

Replacing sendProxy with proxyRequest results in a 404 error from the router

Additional context

No response

Logs

No response

@chrisnoden
Copy link
Author

Any advice on this please? I want to proxy all HTTP methods (GET, POST, PUT, DELETE) to a back-end server and can only proxy GET at the moment. Other methods are changed to GET requests plus any body payload is lost.

@nozomuikuta
Copy link
Member

@chrisnoden

Would you provide a reproduction in which vanilla h3 app without Nuxt is conneted to a publically accesible backend server, so that we could find where the root cause is: h3, Nuxt, or your own infra?


By the way, as far as I tested instantly, h3's proxyRequest works as expected and request information is sent to the backend server without loss (e.g. request body).

Screen Shot 2022-12-27 at 15 49 29

@chrisnoden
Copy link
Author

I don't think the issue is reproducible without Nuxt v3 in the equation.

@chrisnoden
Copy link
Author

After much testing, I think the issue is to do with my local setup. Node fetch or H3 does not like that I am proxying the requests (for dev) to a local docker server which has a Lets Encrypt SSL. Even with NODE_TLS_REJECT_UNAUTHORIZED=0 set the proxyRequest requests don't hit the server - at best I get a 404. I can curl the same request from the dev machine just fine.
I'm still trying out different permutations - but if I modify the target hostname to webhook.site for example it works just fine - but changing only the hostname of the target to a locally resolvable host (my /etc/hosts has the entry) and I get a 404. As mentioned, testing via curl or just using GET requests via sendProxy and it it works.

@chrisnoden
Copy link
Author

I have been able to replace H3 with Express and it works, so I'll stick with that.

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

No branches or pull requests

2 participants