PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, June 25, 2022

[FIXED] How do I make fetch requests to dynamic targets, remote non-cors enabled urls? In http-proxy-middleware, can variables be used in targets?

 June 25, 2022     fetch, http-proxy-middleware, javascript, reactjs, reverse-proxy     No comments   

Issue

I use fetch('htp://list-of-servers') that returns a list of urls

  • test1.example.com
  • test3.example.com
  • test5.example.com

Then I need to perform a fetch() on each one of these urls: fetch('test1.example.com').then(...)

urls.map(url => fetch(url))

but

  • a) url is not cors enabled (so I tried adding them in setupProxy)
  • b) I don't know these urls in advance (since they're result of an earlier fetch, so I'm not sure how they can be added to setupProxy.js. I basically need some kind of equivalent to curl, that can request any non-cors remote url, but in js/react/node.

Suggestions?


Solution

Found a solution online. I have to run an express server/node app at the same time as the react client (different port), so client requests to url=test1.example.com, would be written as fetch(`/api/${url}`) then that would hit the express server localhost for proxying, where it would do a non-browser server-side request fetch(`http://${url}`) where cors problems wouldn't be an issue. I needed an ELI5.



Answered By - Noon Time
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing