CpS 404 Internet App Development

Lab 2: Asynchronous Programming with Node.js

Objectives

  • Learn to use the Node.js debugger
  • Learn to use asynchronous functions in the Node.js API

Prerequisites

  • Review the course slides on the Node.js execution model
  • Skim chapter 7 of Pro Node.js for Developers (see Resources), and chapter 11 pp. 178 - 183.

Instructions

  1. Read about debugging Node.js programs in Visual Studio Code. Then, set a breakpoint, choose Run > Start Debugging, pick Node.js from the list of options, and use the Visual Studio Code debugger to step through your lab1.js program, inspect variable values, etc. 2 points.

  2. Save your lab1.js program as lab2http.js. Then, redesign the program so that it downloads the Dallas City Council data set, uses the CSV module to parse it, and displays the same output as lab1.

    Use the http.get() method to download the data (see the http_request.js example we went over in class). No error handling is needed. 2 points.

  3. Locate the request module on npmjs.org and read about how to use it. Install it using npm, and create a second version of your program named lab2request.js that uses the request module instead of the http module to download the data. Handle errors gracefully. Perform the following error handling tests and document the results in the lab report:

    • Change the URL so that the server name is not found (connect failure)
    • Change the URL so that you get a response 404 (not found)

    3 points.

  4. Create a third version of your program named lab2mydownload.js. In this version, you are to define a function

     function download(url, callback)
    

    This asynchronous function should use the http.get() method to download data from the indicated url and pass the downloaded data to the callback function. The function should check the response.statusCode, and if it is not 200, report an error to the callback using code like this:

    callback(new Error("Received response code: " + response.statusCode))
    

    The function should also detect and report failures to connect, etc., to the callback. Perform the error scenario tests from step 3 above to verify that all of this error handling works correctly, and document these tests carefully in the report.

    Rewrite the main program to use your download() function. 3 points.

Submission

Write a brief lab report indicating what you completed successfully, as well as what (if anything) isn’t working properly. Include screen shots showing the debugger in action (step 1), the output of your programs and results of error tests, and source code listings.

Your report should start with a cover sheet with your name, the assignment title, date, course number. Generate a pdf named report.pdf, and submit via Canvas.