I dont find anything that works for me so I ask here:
I am trying to get some data from a website with axios. It works great, but if I input the wrong URL I get an error. I want to check if the URL with its complete path to the page I desire exists.
I tried npm package url-exists but it just said the path didn't exist eventho it did.
Is there a simple way to check if a path like for example "https://github.com/enterprise" exists or if it doesn't?
You have to make a request anyway to check if the url exists. So you can handle it when you got 404 status code in catch error block of axios function.
The only way is making the request, you can handle this scenario using a try/catch block
const getSomething = async () => {
try {
const something = await axios.get("www.website.fake")
} catch (err) {
throw new Error(err)
}
}
Related
I am getting a proxy error from a PUT action only when its called from a certain page. When I call it from a different page, it works fine.
My code is that doesn't work is:
const startedWorkout = () => {
if (workout?.exercises && selectedWorkout) {
if (selectedWorkout.started && selectedWorkout.completed) {
// restarts workout session
dispatch(toggleWorkoutIncomplete(selectedWorkout));
}
dispatch(startWorkout(selectedWorkout));
dispatch(startExercise(selectedWorkout, workout.exercises));
}
};
When my frontend's URL is http://localhost:8101/workout-started/WKsp5Odze1WuAGm6txWx
All 3 of those functions get the same error:
Proxy error: Could not proxy request /workout/WKsp5Odze1WuAGm6txWx from localhost:8101 to https://us-central1-traineraid-283616.cloudfunctions.net/api.
[react-scripts] See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNRESET).
This is the request for each of the function calls:
axios.put(`workout/${workoutId}`, newValues)
I call the exact same functions that use the PUT request above from a different URL: http://localhost:8101/workouts/list
and it works fine.
I have never seen an error so specific and I have no idea where to start looking for a backend call that doesn't work on specific frontend URLs.
Let me know if i am missing something obvious. I tried to cut down on the code because it would be a lot of stuff to have to copy here but I can post here at any request for more info.
I solved it myself. It turned out that I was navigating to the next page before the functions could load.
I removed the href from the button and changed the code from the onclick function to:
const startedWorkout = () => {
if (workout?.exercises && selectedWorkout) {
if (selectedWorkout.started && selectedWorkout.completed) {
// restarts workout session
dispatch(toggleWorkoutIncomplete(selectedWorkout));
}
dispatch(startWorkout(selectedWorkout));
dispatch(startExercise(selectedWorkout, workout.exercises));
props.history.push(`/workout-started/${workout?.id}`)
}
};
Non-English country, please forgive my spelling mistakes.
For example, I want to first redirect url1(http://localhost:3000/api/song/167278) to url2(http://localhost:4000/api/song/167278) to use url2's api. And url2 will reponse a json file, which can be seen in the postman's panel.
(postman's pannel)
But there maybe a lot of elements, I only want an element in the file, such as data[0].url. How can I return just return the url value (data[0].url in this json) when people access http://localhost:3000/api/song/167278.
I am using express.js now, how can I edit it? Or is there any other methods?
app.get('api/song/:id', async (req, res) => {
try {
const { id } = req.params
url = "http://localhost:4000/api/song/" + id
res.redirect(url)
}
catch (e) {
console.log(e)
}
}
You could either proxy the entire request there or fetch localhost:4000/api/song/1 in your request handler (with something like node-fetch or axios or with node's APIs and send the fields that you want back to the client as json.
So I want to end the request if the user tries to fetch a url which does not work.
This works:
var remote = "https://storage.googleapis.com/ad-system/testfolder/OUTOFAREA.mp3";
var streama = request.get(remote);
however lets say the following mp3 does not exisit
https://storage.googleapis.com/ad-system/testfolder/playme.mp3
When the request.get('https://storage.googleapis.com/ad-system/testfolder/playme.mp3'); tries and fetch the file it will return a 404 error. What I want to do is file not found I want to run res.end();
Any ideas?
you can write like ### request('endpoint url goes here', { json: true }, (err, res, body) => {
if (err) { // keep your business logic here}
}) ######
hope it helps
I doesn't know about this Cloud Storage but even the link isn't accessible so I recommend you check if something is wrong in this service or if you require some special keys or something else to access the content or the service. Then, check your code or use a library that can handle your requirements (if available).
If I found something, I'll let you know :)
EDIT: I can access to https://storage.googleapis.com/ad-system/testfolder/Video/30%20Second%20Timer-0yZcDeVsj_Y.f137.mp4
You could do
app.get("*",(req,res)=> {
res.end()
});
and insert it just before your 404 handler however, I would challenge why that ever might be a good idea. If it's literally so you scan skip checking the return code I really advice you to think twice if thats something you wanna be doing atleast do:
app.get("*",(req,res)=> {
res.status(404)
res.end()
});
I'm trying to make a simple GET request with axios that follows the redirect until network idle, but I can't get the promise to resolve. It seems to work for most urls, but not the below url, specifically - not sure if there's an issue with my code or something peculiar to this one redirect (others of similar format work fine, ie with different mid=XXXX parameters.
A GET request (with no headers or authorization) redirects fine in Postman, but axios won't return anything.
async function axiosGet() {
try {
const response = await axios.get("http://www.awin1.com/awclick.php?mid=4802");
console.log(response);
return response;
} catch (err) {
console.log(err);
}
}
Running this I only get back back my bash prompt with nothing logged. Assigning my axiosGet() function to a variable, and then logging that variable only yields Promise { <pending> }. Just looking to get a resolved promise object, good to go from there.
I'm having a problem right now which I can't seem to find a solution to.
I'm using Uservoice's NodeJS framework to send some requests to UserVoice regarding Feedback posts. A problem I've run into are ratelimits so I want to save the header values X-Rate-Limit-Remaining, X-Rate-Limit-Limit and X-Rate-Limit-Reset locally. I've made a function for updating and getting that value and am calling it like this:
var content = "Test"
c.post(`forums/${config.uservoice.forumId}/suggestions/${id}/comments.json`, {
comment: {
text: content
}
}).then(data => {
rl.updateRL(data.headers['X-Rate-Limit-Limit'],data.headers['X-Rate-Limit-Remaining'],data.headers['X-Rate-Limit-Reset'])
When running this code I get the error Cannot read property 'X-Rate-Limit-Limit' of undefined.
This is not a duplicate, I've also tried it lowercase as described here but had no luck either. Thanks for helping out!
EDIT:
The function takes the following parameters:
module.exports = {
updateRL: (lim, rem, res) {SAVING STUFF HERE}
}
It is defined in the file rates.jsand is imported in the above file as const rl = require('../rates').