Question
Writing N Files Asynchronously [25%] Allowed Node.js API Functions: fs.writeFile() Input: Integer n (0 < n < 100) Output: n Files Create a program that
-
Writing N Files Asynchronously [25%]
- Allowed Node.js API Functions: fs.writeFile()
- Input: Integer n (0 < n < 100)
- Output: n Files
Create a program that asynchronously write n files 01-output.txt up to 99-output.txt to a subdirectory output. Each file should contain the text "Data-1". After each successful write, print a message to console with the filename. After all n files are finished writing, print an additional message Writing Complete. Run the script a few times, the filenames printed should almost never be in order.
A common mistake is to put the print statements inside the same loop that calls fs.write(). This is not correct as the print statements will fire immediately (before a single write has been guaranteed to finish). Put them inside the callback function instead.
Use String.padStart() to pad zeros.
-
Writing N Files Synchronously [25%]
- Allowed Node.js API Functions: fs.writeFile()
- Input: Integer n (0 < n < 100)
- Output: n Files
Create a program that synchronously write n files 01-output.txt up to 99-output.txt to a subdirectory output. Each file should contain the text "Data-2". After each successful fs.writeFile() print a message to console with the filename. After all n files are finished, print an additional message Writing Complete Run the script a few times, the filenames printed should always be in order.
Caution: For this assignment do not use fs.writeFileSync(), the purpose of this question is to show understanding of how to write synchronous code using asynchronous methods. In general check the Allowed Node.js API Functions.
Utility functions that are part of JavaScript (eg. Array.flat, Array.join, Array.split) are allowed. (Anything on MDN is fine)
-
Personal Hosts File [20%]
-
Allowed Node.js API Functions: fs.readFile(), fs.writeFile(), dns.resolve()
-
Input: File: domains.txt
- Contains one valid domain name on each line
- All supplied domains will only have a single IP addresses associated with it.
-
Output: File hosts.txt
Write a program that reads from a file domains.txt residing in a directory input. It contains a list of valid domains one on each line, resolve each domain found to IP addresses, and save the results into a file hosts.txt residing in a directory output. The output format should be ip_address, a tab character (\t), domain_name.
The order in which the results appear does not matter. (But see Ungraded Additional Question)
Sample Output:
149.4.199.190 venus.cs.qc.cuny.edu
149.4.211.163 cs.qc.cuny.edu
Possibly helpful utility Functions
String.split(" ")
Ungraded Additional Question
The order that the IP addresses come back is not guaranteed to match the order the domains came in, however a minor change can be made to your code such that this becomes true. Refer to Example 10: Preserving Order
-
-
Four synchronous tasks [20%]
- Allowed Node.js API Functions: fs.readFile(), zlib.inflate(), dns.resolve(), fs.writeFile()
- Input: domain.deflated
- Output: File: ip_address.txt
Write a program that:
- Reads domain.deflated (Use {encoding:null} ),
- Decompresses the contents using zlib.inflate(), covert the resulting buffer to a string using .toString("utf8") the decompressed data will be a valid domain that resolves to a single IP address.
- Using dns.resolve() convert the domain into an IP address.
- Remove the array wrapper and write the IP address to a file ip_address.txt
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started