GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com โ JavaScript Example Code JavaScript Example Code
MMDRZA edited this page
This example demonstrates how to fetch and display cryptocurrency data from a JSON source using Node.js and JavaScript. This JavaScript script fetches cryptocurrency data from a given URL and logs details such as the symbol, last price, high price, low price, change rate, and the last updated time for each cryptocurrency. The script uses Node.js's built-in
const https = require('https'); // URL of the raw JSON file const rawJsonUrl = 'https://raw.githubusercontent.com/Crypto-Static/Rate/main/rateStatic.json'; /** * Fetch JSON data from a given URL using a GET request. * * @param {string} url - The URL to fetch data from. * @returns {Promise<Object>} A promise that resolves with the JSON data or rejects with an error. */ function fetchJsonData(url) { return new Promise((resolve, reject) => { https.get(url, (res) => { let data = ''; // Concatenate data as it comes in chunks res.on('data', (chunk) => { data += chunk; }); // When the response is complete, parse the JSON res.on('end', () => { try { const jsonData = JSON.parse(data); resolve(jsonData); } catch (err) { reject(new Error('Error parsing JSON: ' + err.message)); } }); }).on('error', (err) => { reject(new Error('Failed to fetch data: ' + err.message)); }); }); } /** * Display information about a cryptocurrency coin. * * @param {Object} coin - An object containing coin data. */ function displayCoinInfo(coin) { const { symbol, lastPrice, highPrice24h, lowPrice24h, changeRate, lastUpdated } = coin; console.log( `Symbol: ${symbol} | LAST: ${lastPrice} | HIGH: ${highPrice24h} | LOW: ${lowPrice24h} | CHANGE: ${changeRate} | UPDATED: ${lastUpdated}` ); } // Main execution block (async () => { try { // Fetch JSON data from the specified URL const coinDataList = await fetchJsonData(rawJsonUrl); // Loop through each coin's data and display the relevant information coinDataList.forEach(displayCoinInfo); } catch (err) { console.error('Error:', err.message); } })();
๐จโ๐ป Programmer : PyMmdrza ๐ Official Website Mmdrza.Com ๐ง Email : Clone this wiki locally |
| You are visitor 01402503 โ GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800ร600. No cookies required. |