#npm request deprecated
76 messages · Page 1 of 1 (latest)
Use fetch
How do you suggest I use it?
Wait the opld fetch or node-fetch
I'm unsure of which
Fetch is built in on nodejs version 18+ I believe, if you have an older version then sure you can use node-fetch. It's like, the same thing as request tbh
fetch(url, options);
//options is an object as follows:
options = {
method: //GET, PUT, PATCH, etc
headers: //Object of headers
body: //Body of request
};
/*
fetch(url) is the same as
fetch(url, { method: "GET" })
*/
Okay, got ya do i need to uninstall the old fetch i just installed?
I don't think it would mess with the built in fetch, you can though because like, you're not gonna use it
https://www.scaler.com/topics/nodejs/node-js-fetch/
Here's a site about it
Ofc, you can check the nodejs website about fetch
How do i uninstall npm lol, sorry...I havent coded in a while
npm uninstall <package> I believe should remove the package
You can log what the promise returns, don't remember it off the top of my head, there's also helpful methods like
result.json() and result.text(), check the nodejs docs on it
Do you think axios is any better
I'm just a little confused on how to use fetch to do what request was doing honestly (im reading the docs, dont worry)
I don't have much experience with axios but I would think a built in function is better than a 3rd party
And I'm not sure why i keep saying nodejs, fetch is a js thing
Got ya
Lemme get the docs on it for you
In your code, you're sending a GET request, this is what you'd have to do:
const reddit = await fetch(subreddit);
const structure = await res.json();
That's the whole fetching part of your code but using the fetch api
reddit would be a Response class
structure would be the json object the url returns
Fetch API
The Fetch API provides an interface for fetching resources (including across the network). It is a more powerful and flexible replacement for XMLHttpRequest.
Try creating a test file using this and do node <test-file>.js
And log the return values
Okay, one moment.
Also thanks os much for helping me out
What would I console.log()?
The structure?
Whatever you'd like to see, if you want to see watch fetch returns console log reddit. If you'd like to see what the json method returns console log structure
The json method returns the json version of the body, if that makes sense
Dammit, i might give up on this. im a little confused now
You're just logging whatever you want to see
Since you're not sure on how it works, test it out and see what each function returns and stuff
I thought "await" can only be used in asynchronous functions?
Or at the top level body of modules but yeah, jus put it in an async function then,
(async() => {
//code
})();
And run the test file
oksy hang tght
Do I make subreddit a variable?
Yeah, you can make the code however you want I'm just allowing you to view what the functions return
fetch(url) returns a promise, that promise resolves the Response class. fetch(url) is a GET request which is just like request.get()
response.json() transforms the body of the response to a json object
It also returns a promise
Like this right>
Well, Response isn't defined
Change that to reddit
oop
reddit is your Response class, cuz that's what fetch returns and you stored that into the reddit variable
OHHHHH
I was lost on that but now I got you
You're logging the response class, when you run the file you should see a bigass object log into your console. That's the response class
To log the json, log structure
Not sure what this means
Whatever link you're fetching does not return a json
But rather an html
OH
Nvm i know how to fix that
one sec
Fetch the same hentai link you used here, if it contains any nsfw stuff you shouldn't send it but this does return a json
i can add /new.json to get the json
Okay, idk what any of these values are
but im getting somwhere
Did you log structure?
If you did, then that's what the link you're fetching returns, that's the json
yeah
Click the link and you can view it on your browser yourself to see
I can't help you further with anything on that site because I dont know what it contains, but you just learnt how to make a GET request using fetch and how to transform the response's body to a json
If the body of the response is not a json, you will get that error youve gotten just now
That's basically it, you've done exactly what you did using request but with fetch
What if I did something like let posts = structure.data.children;
Then posts would be the array of objects you see under the data.children property in the log
structure is a normal object
Idk why I kept saying convert to a json, it converts the json in the response body to a normal object. That's basically it
Imms head to bed but need more helping, ill update