+ 5
Fetching data with Node.js.
I'm trying to fetch data with Node.js, from the official api of SoloLearn (v2.0), and to make some usable variables containing this info. In JavaScript (web playground) it is possible, is it possible using the same syntax in node.js too? I tried to make it, but I got some problems but I don't know how to fix it and if it's possible to fetch data. I'll link the code here below. Thanks a lot on advance :D https://code.sololearn.com/cZUhGDCY0A8n/?ref=app
17 odpowiedzi
+ 3
or this one:
https://www.valentinog.com/blog/http-js/
(make a google search with "fetching data with nodejs")
+ 4
the first way presented in second link use native http.get api ;)
+ 4
visph yeah sorry a lot, I was a bit confused, but at the end you're right, thanks for telling!
You really helped me, sorry for the misunderstanding.
I thought he used a totally different method, so I got really confused, btw let me mark your answer as the best, it deserves it! :D
Sorry again for the misunderstanding and thanks for the help! :D
+ 3
As far as I know, you can't install node modules on Sololearn because you don't have access to the normal Node runtime environment or config. This means you don't have NPM either. You can only require a module such as node-fetch if you've already installed it using a package manager such as NPM or Yarn.
You may be able to bring in node-fetch via a script from a CDN source but that could get messy.
This is why as visph has said, using the http module which comes natively with Node might the best way to do it on Sololearn:
const https = require("https")
// and so on
+ 3
I can see why it would be surprising/annoying that some commonly used features don't exist in Node.
That said, XMLHttpRequest is a web API so it's not actually part of standardised ECMAScript. It's just a client-side web API implemented by browsers, albeit a very common one.
+ 2
this link could be helpful:
https://stackabuse.com/making-http-requests-in-node-js-with-node-fetch/
+ 2
https://code.sololearn.com/c4kr0bXzJecE/?ref=app
+ 2
Major シ
How could Calviղ answer deserve the best answer mark by coming after the battle with a code using the native api you've used one day before?...
I'm quite sure he had the good answer by himself, but how did he helped here? ;P
I can difficult understood why you don't marked mine second link providing the native api solution, but I accept it as it is your choice, but marking somebody else providing the solution you finally used one day before... I definitively don't understand ^^
+ 2
Your options are generally to either import a node module (npm install...) if you can, or, baring that, use the node request module:
https.get(yourUrlHere,function(result){
//do stuff with result here;
});
Unfortunately, this method won't natively work with promises (unless you promisify https . get), so you'll need to get a little fancy to use async/await here.
EDIT: Take a look at this playground. I've included async/await here: https://code.sololearn.com/c99a7a25A15A
+ 1
I don't think XMLHttpRequest exist too in NodeJS ^^
+ 1
the error says "cannot find module 'node-fetch'"... meaning it's not available in sololearn :(
have you tried the second link wich would show alternatives?
+ 1
visph I made bow a lot of tests....
Here the codes:
https://code.sololearn.com/cE68DzauysoZ/?ref=app
https://code.sololearn.com/c2s7oGb8rVUj/?ref=app
https://code.sololearn.com/c7tTr9DjxM04/?ref=app
https://code.sololearn.com/c13Ex44UCULf/?ref=app
Woah and the last works (the other 4 failed xD), thanks for the help! :D
Woah finally :D I'm ultra happy, thanks again!
https://code.sololearn.com/cjQ0isHDxKFP/?ref=app
+ 1
Thanks CamelBeatsSnake!
Now I resolved it :D
+ 1
I've also never really used node... except for running basic js scripts through playgrounds :D
I've just guessed because XMLHttpRequest was part of (client side) web api, and I know node doesn't implement (client side) web api... and I've read something about that recently in sololearn Q&A ;)
0
visph yeah, I already Google it before asked, but I got stuck again, with errors, even following the descriptions.
Maybe only SoloLearn doesn't support it?
https://code.sololearn.com/cQZTgQFAxuLj/?ref=app
0
Mirielle[ Exams ] oh ok, I'll search about it, thanks a lot :D
visph omg, then I'll just let it go, really irritating.
Btw thanks a lot everyone :D