Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Writing N Files Asynchronously [25%] Allowed Node.js API Functions: fs.writeFile() Input: Integer n (0 Output: n Files Create a program that asynchronously write n files

Writing N Files Asynchronously[25%]

  • Allowed Node.js API Functions: fs.writeFile()
  • Input: Integer n (0
  • Output: n Files

Create a program thatasynchronouslywrite 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 shouldalmost neverbe 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.

  1. Writing N Files Synchronously[25%]
  • Allowed Node.js API Functions: fs.writeFile()
  • Input: Integer n (0
  • Output: n Files

Create a program thatsynchronouslywrite 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 shouldalwaysbe in order.

Caution: For this assignmentdo not usefs.writeFileSync(), the purpose of this question is to show understanding of how to write synchronous code using asynchronous methods. In general check theAllowed 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)

  1. 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

  1. 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java Programming

Authors: Joyce Farrell

9th edition

1337397075, 978-1337397070

More Books

Students also viewed these Programming questions

Question

Organizational buyers are ________.

Answered: 1 week ago

Question

What is the work environment like? Friendly/collegial?

Answered: 1 week ago