+ 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

24th Mar 2021, 4:48 PM
Matthew
Matthew - avatar
19 Respuestas
+ 3
or this one: https://www.valentinog.com/blog/http-js/ (make a google search with "fetching data with nodejs")
24th Mar 2021, 5:02 PM
visph
visph - avatar
+ 4
the first way presented in second link use native http.get api ;)
24th Mar 2021, 6:18 PM
visph
visph - avatar
+ 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
26th Mar 2021, 1:31 PM
Matthew
Matthew - avatar
+ 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
24th Mar 2021, 6:32 PM
CamelBeatsSnake
CamelBeatsSnake - avatar
+ 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.
24th Mar 2021, 8:58 PM
CamelBeatsSnake
CamelBeatsSnake - avatar
24th Mar 2021, 5:00 PM
visph
visph - avatar
+ 2
https://code.sololearn.com/c4kr0bXzJecE/?ref=app
24th Mar 2021, 11:04 PM
Calviղ
Calviղ - avatar
+ 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 ^^
26th Mar 2021, 12:23 PM
visph
visph - avatar
+ 2
Major シ I am sorry, I shared my old code here and I didn't notce you have found the similar solution, I think visph has given a good guidance deserve a best answer.
27th Mar 2021, 12:54 AM
Calviղ
Calviղ - avatar
+ 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
28th Mar 2021, 8:23 PM
HealyUnit
HealyUnit - avatar
+ 1
I don't think XMLHttpRequest exist too in NodeJS ^^
24th Mar 2021, 5:53 PM
visph
visph - avatar
+ 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?
24th Mar 2021, 6:16 PM
visph
visph - avatar
+ 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
24th Mar 2021, 6:31 PM
Matthew
Matthew - avatar
+ 1
Thanks CamelBeatsSnake! Now I resolved it :D
24th Mar 2021, 6:34 PM
Matthew
Matthew - avatar
+ 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 ;)
24th Mar 2021, 8:09 PM
visph
visph - avatar
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
24th Mar 2021, 6:13 PM
Matthew
Matthew - avatar
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
24th Mar 2021, 6:14 PM
Matthew
Matthew - avatar