Ky: Using with http-proxy-middleware

Created on 30 Sep 2019  路  6Comments  路  Source: sindresorhus/ky

Hi. I have problem with ky.js and proxy. No requests in browser when i use ky.create and prefixUrl

I tried changes url in prefix like localhost:3000/api or localhost:3000 and in

const terminalsUrl = 'api/terminals';
or like this
const terminalUrl = '/terminal';

ky.config.js
image

proxy.server.js
image

Most helpful comment

Perfect, thanks for the repro. I forked your gist, fixed the problems, and pushed the changes. I also ran the proxy and verified the fix.

https://gist.github.com/sholladay/7316505d5f7cc5a9fc596973e1cb4c7d/revisions

There were two problems with the code:

  1. Your prefixUrl did not include /api/v1/. Thus, the request was going to http://localhost:3000/employees instead of http://localhost:3000/api/v1/employees. The proxy was returning a 404 Not Found error, because that's what http://dummy.restapiexample.com/employees returns to the proxy for a JSON request. The correct URL is http://dummy.restapiexample.com/api/v1/employees. I just added the API prefix to the prefixUrl.
  2. In index.js, you were _returning_ the console.log function and not calling it. That means even if the request succeeded, you never would have seen the data. I changed it to .then(console.log), which is equivalent to .then(data => console.log(data)).

With those changes, I was able to log the employees data successfully.

If you need any further help on this, the best course of action would be to ask a question on StackOverflow and post the link here. In the meantime, I'm going to close this, as it's not a problem with Ky.

Good luck! 馃檶

All 6 comments

None of your URLs in the screenshot points to /api where your proxy listens.

@popuguytheparrot can you please show a full reproducible code example that I can run on my own machine? Otherwise it's hard to debug this.

From just the snippets you posted, it sounds like maybe ky is never actually getting called.

I'm curious what will happen if you do:

const terminalsUrl = 'api';

I think @kevva made a good point that your input URL is probably not matching your proxy route on the backend. I don't know if your proxy is handling only /api, or if it's handling /api/* (all paths beginning with /api/). It's worth double-checking that it's the latter and not the former, otherwise trying to fetch /api/terminals on the frontend will not work.

http-proxy-middleware should catch all routes starting with /api. However, I'm pretty sure this is not an issue with ky.

Perfect, thanks for the repro. I forked your gist, fixed the problems, and pushed the changes. I also ran the proxy and verified the fix.

https://gist.github.com/sholladay/7316505d5f7cc5a9fc596973e1cb4c7d/revisions

There were two problems with the code:

  1. Your prefixUrl did not include /api/v1/. Thus, the request was going to http://localhost:3000/employees instead of http://localhost:3000/api/v1/employees. The proxy was returning a 404 Not Found error, because that's what http://dummy.restapiexample.com/employees returns to the proxy for a JSON request. The correct URL is http://dummy.restapiexample.com/api/v1/employees. I just added the API prefix to the prefixUrl.
  2. In index.js, you were _returning_ the console.log function and not calling it. That means even if the request succeeded, you never would have seen the data. I changed it to .then(console.log), which is equivalent to .then(data => console.log(data)).

With those changes, I was able to log the employees data successfully.

If you need any further help on this, the best course of action would be to ask a question on StackOverflow and post the link here. In the meantime, I'm going to close this, as it's not a problem with Ky.

Good luck! 馃檶

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rohmanhm picture rohmanhm  路  6Comments

lkostrowski picture lkostrowski  路  7Comments

nnals picture nnals  路  4Comments

fgreinus picture fgreinus  路  6Comments

jacob-fueled picture jacob-fueled  路  3Comments