File Assignment

Video

Overview

The purpose of this assignment is to learn how to read files into buffers and deliver their contents to requesting clients.

Assignment folder

Create a directory named file for the work you do for this assignment. At the end of the assignment, this folder will contain the following files.

Step 1

This assignment builds on the work you did for the buffers assignment. For this reason, start by creating a folder named file and then copy the contents of the buf folder into it.

Verify that the code you copied into the buf folder runs.

Step 2

Create a file named app.html and add some simple HTML to it. You will write code that reads the contents of this file when the server starts and returns this data to browsers that request the resource associated with the root path /.

Our strategy will be to read the file app.html into a buffer when the server starts, and return the contents of the buffer for each request sent by browsers the root path /. This procedure is more efficient than reading the file from the file system for each request; however, it requires us to restart the server to test changes to the file.

Replace the contents of file root.js with the following. This code uses the readFile method of the fs module to read the contents of app.html into an instance of Buffer. This is done in a function named init. This function must be called from the main module when the server starts.

var http = require('http');

var body;

exports.handle = function(req, res) {
  res.writeHead(200, {
    'Content-Type': 'text/html; charset=UTF-8',
    'Content-Length': body.length
  });
  res.end(body);
};

exports.init = function(cb) {
  require('fs').readFile('app.html', function(err, data) {
    if (err) throw err;
    body = data;
    cb();
  });
}

Modify main.js so that it calls the exported init function in the root module before the server starts listening to port 5000. To accomplish this, you need to run the command to listen to port 5000 inside a callback function that you pass into init.

Run and test with browser.

Step 3

This part of the assignment includes substantially more work than the previous parts and so will require more time to complete.

You need to create a test image in png format for this part of the assignment. Call the image image.png.

Add an img element to your web page that loads an image named image.png from your server. When the browser finds an img tag in an html document, it immediately executes another HTTP GET request for that image.

There are many ways to return the image from the server, but for this assignment you should create a module similar to root.js that does the job. Call the module file image.js. In addition to creating the image module, you will need to modify the main module to call the handle function of the image module when the url of the request object equals /image.png.

When you send your image data to the client, set the Content-Type to image/png.

Similar to the root module, the image module will have an init function that reads file contents into memory when the server starts. Add code to the main module so that the init functions of the root and image modules run in parrallel. Also, make sure that you do not start listenning to connection requests until both init function complete. You should review the parallel code you developed for the callbacks assignment to help you solve this problem. By running the init functions in parallel, your server will be ready to process requests as early as possible.

Run and test with a browser.