node js read stream, write to a file and calculate hash
By | 6 months ago
In the JavaScript file, write a program to perform a GET request on the route https://coderbyte.com/api/ challenges/json/age-counting which contains a data key and the value is a string which contains items in the format: key STRING, age INTEGER. Your goal is to count how many items exist that have an age equal to 32. Then you should create a write stream to a file called output.txt and the contents should be the key values (from the json) each on a separate line in the order they appeared in the json file (the file should end with a newline character on its own line). Finally, then output the SHA1 hash of the file.
11
12
13
14
15
16
Example Input
["data":"key=IAfpK, age=32,
key WNVdi, age=64, key=jp9zt,
To complete your request, I'll provide a Node.js script that:
-
Makes a GET request to the specified API URL.
-
Parses the response to count how many items have an age of 32.
-
Writes the keys of all items to a file called
output.txt
, each on a new line. -
Calculates and prints the SHA1 hash of the file contents.
Here's the complete Node.js script to achieve this:
const https = require('https'); const fs = require('fs'); const crypto = require('crypto'); // Function to fetch data from the API function fetchData(callback) { const url = 'https://coderbyte.com/api/challenges/json/age-counting'; https.get(url, (res) => { let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { callback(data); }); }).on('error', (err) => { console.error('Error fetching data: ', err); }); } // Function to process data and write to file function processData(data) { const result = JSON.parse(data); const items = result.data.split(', '); const outputStream = fs.createWriteStream('output.txt'); let age32Count = 0; items.forEach((item) => { const [keyPart, agePart] = item.split(', age='); if (agePart && parseInt(agePart) === 32) { age32Count++; } const key = keyPart.split('=')[1]; outputStream.write(`${key}\n`); }); outputStream.end('\n'); console.log(`Number of people aged 32: ${age32Count}`); outputStream.on('finish', () => calculateHash('output.txt')); } // Function to calculate SHA1 hash of the file function calculateHash(filename) { const hash = crypto.createHash('sha1'); const stream = fs.createReadStream(filename); stream.on('data', (data) => { hash.update(data); }); stream.on('end', () => { const fileHash = hash.digest('hex'); console.log(`SHA1 Hash of the file: ${fileHash}`); }); } // Main function to run the process function main() { fetchData(processData); } main();
Breakdown:
-
**fetchData**: This function fetches data from the provided API endpoint.
-
**processData**: This function processes the data to find keys, counts ages equal to 32, and writes the keys to
output.txt
. -
**calculateHash**: This function calculates the SHA1 hash of the output file.
-
**main**: Orchestrates the entire process.
Ensure you have Node.js installed on your system to run this script. Save the script in a file and run it using the command node <filename.js>
.