how to modify server relative url in response - node.js

I use http.request a URL and put the response to a new response in Nodejs. But the response of the http request has some server relative url inside, for the links, resources etc.
how can I make the relative url becomes a full url, then I can display the entire content in nodejs response. Thanks.

Related

Express routing giving 404 when receving params

I'm working with Express Router, and I'm trying to get some information from params URL, but I cannot make it works as it always returned me a 404.
This is the URL I'm trying to call
http://localhost:8080/api/v1/contr/method?param1=value1&param2=param2
And in my express router I have:
this.router.get("/contr/method", JWT.authenticateJWT, ContrController.method.bind(ContrController));
And I always get
finalhandler default 404 +2m
If I send the request without params the app work as expected:
http://localhost:8080/api/v1/contr/method
If you're building a URL with a URL itself as a parameter, then you need to call encodeURIComponent() on the embedded URL:
let url = "http://localhost:8080/api/v1/contr/method?url=" + encodeURIComponent(otherUrl);
This encodes the parameter in a way that will not be confused with the path of the URL during URL parsing. See doc on encodeURIComponent() for more info.
You need to use encodeURIComponent() on any parameter that contains characters that are special when it comes to URL parsing. They will then be encoded in hex such as %2F which will cause them to not match any of the URL parsing. And, the URL parsing in Express already automatically decodes them for you.

How to use res.redirect to redirect to a data url?

I need to use the res.redirect() to redirect to a data URL using express.
This is my code...
app.get("/",(req,res)=>{
res.redirect("data:text/plain;base64,hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
})
When I use another URL eg: https://www.google.com It works fine.
But when I use a data URL it is not working..
How should I fix it?
I am new to programming and StackOverflow. So, please be kind to me
You could redirect the user to a blob of the dataurl location.
For that just search google for dataurl to blob in js etc.
And also as you are using nodejs you could use the http.request and pipe the response to client just like proxying.
or you could fetch that file with "http.request" and save it in your server and give it when the client needs it
This is likely Chrome issue rather than NodeJS:
https://groups.google.com/a/chromium.org/g/blink-dev/c/GbVcuwg_QjM/m/GsIAQlemBQAJ
In practice, these will be blocked:
Navigations when the user clicks on links in the form of
window.open(“data:…”)
window.location = “data:…”
Meta redirects

How to prevent React-router from messing up your API url

I have a POST endpoint set up like so:
// from server.js
const photo = require('./routes/imagesRoute');
app.use("/api/photo",photo);
//imagesRoute.js
router.route('/photos/:id') // final path will thus be api/photo/photos/:id
.post(employeePhotos)
On the frontend, the component from which the request is made has the following path :
http://localhost:3000/employeePhoto/5fc8a739a89f461274a6286f
I am using axios to POST the request like so:
axios.post(`api/photo/photos/${this.state._id}`,formData )
The challenge is that whenever i make a request to the endpoint 'api/photo/photos' i get a 404 response like so :
POST /employeePhoto/api/photo/photos/5fc8a739a89f461274a6286f 404 22.239 ms - 195
obviously this will not match the endpoint url hence the 404! How would i fix this mix up?
It happens beacause your url in axios is relative, you use this:
axios.post(`api/photo/photos/${this.state._id}`,formData )
I would suggest tu put a "/" before the url:
axios.post(`/api/photo/photos/${this.state._id}`,formData )
When not starting the url with /, it will be relative to whatever your path is currently. I solved the same issue based in this concept, more information
StackOverflow ReactJs...

Redirect in Flask - pass a custom header in the request to target

TLDR; How do I set a custom HTTP request header in redirect?
Below code redirects to an external service but fails to pass 'Authorization' request header to target. I can see the header in response before redirect happens - can it be forwarded somehow?
#app.route('/external_page')
def external_page():
if not request.cookies.get('id_token'):
return flask.redirect(flask.url_for('login',_external=True,_scheme=SCHEME))
response = flask.redirect(flask.url_for('https://external_service_url/v1/ui'))
response.headers['Authorization'] = 'token_id'
return response
Answer:
It's not possible, in HTTP and not in any framework.
The only way for a site to instruct a browser to issue an HTTP request
with a custom header is to use Javascript and the XMLHttpRequest
object. And it needs CORS implemented on the target server to allow
such ajax requests.
Source: Redirect to page and send custom HTTP headers
It's not possible
See Redirect to page and send custom HTTP headers
The only way for a site to instruct a browser to issue an HTTP request
with a custom header is to use Javascript and the XMLHttpRequest
object. And it needs CORS implemented on the target server to allow
such ajax requests.

how to get the landing page URL after redirections using axios

Using NodeJS, If I use axios with maxRedirects = 5, If I type a URL that will be redirected to another URL, how can I get the URL from the final landing page?
In HTTP headers, when there is an HTTP 200 code, there is no header field for the landing page.
Example: if I use:
axios.get('http://stackoverflow.com/',config)
.then(function(response) { console.log(response);}
axios will automatically redirect to https://stackoverflow.com. So, how can get the final URL value "https://stackoverflow.com"?
Should I investigate in the returned object "response" and recreate the full url from domain and URI?
Here is my quick and dirty solution on NodeJS. The starting URL is http://www.stackoverflow.com/questions/47444251/how-to-get-the-landing-page-url-after-redirections-using-axios , the final landing URL is
https://stackoverflow.com/questions/47444251/how-to-get-the-landing-page-url-after-redirections-using-axios Please find the trick below to get the landing URL.
axios.get('http://www.stackoverflow.com/questions/47444251/how-to-get-the-landing-page-url-after-redirections-using-axios').then(function(response) {
console.log(response.request.res.responseUrl);
}).catch(function(no200){
console.error("404, 400, and other events");
});
response.request.res.responseURL is the right field in the JSON object that is returned by axios. If you run axios from a browser use console.log(response.request.responseURL);
I solved it by getting it from:
response.request.res.responseUrl
The Nicholas answer failed to get the port (like localhost:8080)
I solved it by getting it from:
response.request.responseURL
The MrFabio's answer failed for me.

Resources