I am looking for the Node.js of the following PHP Script:
$SMA_APICall = "https://www.alphavantage.co/query?function=SMA&symbol=".$symbolValue."&interval=15min&time_period=10&series_type=close&apikey=R3MGTYHWHQ2LXMRS";
$SMAresponse = file_get_contents($SMA_APICall);
$jsonSMA = json_encode( $SMAresponse);
Here, I am trying to make a call to an API. The API call returns a json object. I want to repeat the same thing using Node js
I believe what you're trying to do is making a request to an API and get the JSON data. Here's how you can do it with native Node.js module https
const https = require('https');
https.get(`https://www.alphavantage.co/query?function=SMA&symbol=${symbolValue}&interval=15min&time_period=10&series_type=close&apikey=R3MGTYHWHQ2LXMRS`, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
console.log(JSON.parse(data)); // JSON Data Here
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
There're several other ways you can do this with other simpler packages. I highly recommend axios
because it's cleaner and easier.
The full examples please refer to this article
Take a look at the request
library: https://github.com/request/request
var request = require('request');
var url = "https://www.alphavantage.co/query?function=SMA&symbol=" + symbolValue + "&interval=15min&time_period=10&series_type=close&apikey=R3MGTYHWHQ2LXMRS";
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var jsonSMA = JSON.parse(body);
// Continue code here
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.