Difference between revisions of "WebFund 2016W: Assignment 2"

From Soma-notes
Jump to navigation Jump to search
Line 22: Line 22:


<source lang="javascript">
<source lang="javascript">
     var Console = require('console').Console;
     var Console = require('console').Console;


var logStream = logStream = fs.createWriteStream("log.txt", {'flags': 'a'});
    var logStream = logStream = fs.createWriteStream("log.txt", {'flags': 'a'});


if (!logStream) {
    if (!logStream) {
  logStream = process.stdout;
      logStream = process.stdout;
}
    }


var myConsole = new Console(logStream);
    var myConsole = new Console(logStream);
    
    
myConsole.log("This will be written to a file, unless there was a problem!");
    myConsole.log("This will be written to a file, unless there was a problem!");
</source>
</source>



Revision as of 18:15, 24 January 2016

This assignment is not yet finalized.

In this assignment you will be be extending the functionality of tinywebserver.js from Tutorial 2. There are ?? points in ?? tasks. This assignment is due on January 27, 2016.


Your modified version of tinywebserver.js should have the following functionality:

  1. Add a new 777 status code if there is a request for any document with the word "lucky" in its URL (path or document name).
  2. Add a command line argument which is the name of a configuration file. This file should contain the contents of the options object. Note that tinywebserver.js should start up correctly without this option file being specified; in this case it should behave as it did originally.
  3. In the configuration file add the following properties to the option object that change the behavior of the app as follows (note it should also contain all the options that were already part of the options object):
    • logged-headers: an array of headers (as strings) that should be logged for each incoming request. If no logged headers are specified, no headers should be logged.
    • logfile: the filename where to write log messages. They should be written to standard out (as console.log does) if no logfile is specified.
    • errorpage: html file to be returned when an error page is returned (not 200). The 777 status code should NOT return an error page, it should return the regular document (777 is an alias for 200).
    • aliases: object where the key is the alias and the value is what should be substituted. The alias should be for the entire doc path (the URL without the hostname).

Hints

  • To log everything to a file you'll want to use a custom Console object with a special write stream. To get append-only behavior (as logs shouldn't normally be erased) do something like this:
    var Console = require('console').Console;

    var logStream = logStream = fs.createWriteStream("log.txt", {'flags': 'a'});	

    if (!logStream) {
       logStream = process.stdout;
    }

    var myConsole = new Console(logStream);
  
    myConsole.log("This will be written to a file, unless there was a problem!");
  • JSON.parse() is very strict with its parsing, much stricter than regular object declarations in JavaScript. In particular properties need to be quoted (only with double quotes). You can use try/catch to allow this to be handled gracefully:
 try {
   options = JSON.parse(fs.readFileSync(optionsFilename, "utf-8"));
 } catch (e) {
   if (optionsFilename) {

console.log("Error reading/parsing options file " + optionsFilename + ", using defaults.");

   } else {

console.log("No options file specified, using defaults.");

   }
   options = default_options;
 }