COMP 2406: Fundamentals of Web Applications. Fall 2013 Mid-Term Exam Solutions

Similar documents
COMP 2406: Fundamentals of Web Applications. Winter 2014 Mid-Term Exam Solutions

COMP 2406: Fundamentals of Web Applications Winter 2014 Final Exam Solutions Instructor: Anil Somayaji April 24, 2014

Node.js. Node.js Overview. CS144: Web Applications

Practical Node.js. Building Real-World Scalable Web Apps. Apress* Azat Mardan

Backend Development. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Catbook Workshop: Intro to NodeJS. Monde Duinkharjav

Django with Python Course Catalog

Hands-on Lab Session 9011 Working with Node.js Apps in IBM Bluemix. Pam Geiger, Bluemix Enablement

We are assuming you have node installed!

Express.JS. Prof. Cesare Pautasso Modularity

Getting MEAN. with Mongo, Express, Angular, and Node SIMON HOLMES MANNING SHELTER ISLAND

Tools. SWE 432, Fall Design and Implementation of Software for the Web

This course is designed for web developers that want to learn HTML5, CSS3, JavaScript and jquery.

Backend Development. SWE 432, Fall Web Application Development

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

Introduction to Express.js. CSC309 Feb. 6, 2015 Surya Nallu

"Charting the Course... Comprehensive Angular. Course Summary

IERG 4210 Tutorial 08

Microservices with Node.js

Web Site Development with HTML/JavaScrip

INF5750. Introduction to JavaScript and Node.js

JavaScript & DHTML Cookbool(

Excel Programming with VBA (Macro Programming) 24 hours Getting Started

Client Side JavaScript and AJAX

Report Submission User s Manual

How to Guide. Create a Data Set. Version: Release 3.0

IN PRACTICE. Daniele Bochicchio Stefano Mostarda Marco De Sanctis. Includes 106 practical techniques MANNING

"Charting the Course... WebSphere Portal 8 Development using Rational Application Developer 8.5. Course Summary

FALL 2017 CS 498RK JAVASCRIPT. Fashionable and Functional!

1 CUSTOM TAG FUNDAMENTALS PREFACE... xiii. ACKNOWLEDGMENTS... xix. Using Custom Tags The JSP File 5. Defining Custom Tags The TLD 6

"Charting the Course... MOC C: Querying Data with Transact-SQL. Course Summary

Unifer Documentation. Release V1.0. Matthew S

Web Application Development

Follow Me an Android Application

MARYLAND HOSPITAL DATA REPOSITORY DATA COLLECTION SYSTEM

Static Webpage Development

The 4D Web Companion. David Adams

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

"Charting the Course to Your Success!" MOC D Querying Microsoft SQL Server Course Summary

Persistence & State. SWE 432, Fall 2016 Design and Implementation of Software for the Web

JavaScript: Sort of a Big Deal,

IP Camera Installation Brief Manual

Step 1: Download the Overdrive Media Console App

SYMBIOSIS LAW SCHOOL, NOIDA Learning Resources - Library

Remote Access Guide.

Kentico CMS User s Guide 4.1

"Charting the Course... Intermediate PHP & MySQL Course Summary

"Charting the Course... MOC A Introduction to Web Development with Microsoft Visual Studio Course Summary

"Charting the Course... MOC A: Developing with the SharePoint Framework. Course Summary

Course Outline. ProTech Professional Technical Services, Inc. Comprehensive Angular 7 Course Summary. Description

Introduction to PTC Windchill PDMLink 11.0 for Heavy Users

Corrective Action User Walkthrough: New Portal Login

Introduction to PTC Windchill PDMLink 11.0 for the Implementation Team

extc Web Developer Rapid Web Application Development and Ajax Framework Using Ajax

2.) ilit Welcome Screen

Agenda. Simple precursor to Sessions. Sessions via Cookies in Hapi

Computers for Beginners

Web development using PHP & MySQL with HTML5, CSS, JavaScript

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

DreamFactory Customer Privacy and Security Whitepaper Delivering Secure Applications on Salesforce.com

Microsoft Visual C# Step by Step. John Sharp

Understanding Angular Directives By Jeffry Houser

SynApp2 Walk through No. 1

jquery Cookbook jquery Community Experts O'REILLY8 Tokyo Taipei Sebastopol Beijing Cambridge Farnham Koln

Kalaivani Ananthan Version 2.0 October 2008 Funded by the Library of Congress

Benter Niu(Sales Manager&Software Engineer) Skype ID:benter.niu WhatsApp/TEL.: CONTENTS

Computer Programming C++ (wg) CCOs

The course is supplemented by numerous hands-on labs that help attendees reinforce their theoretical knowledge of the learned material.

JavaScript: the language of browser interactions. Claudia Hauff TI1506: Web and Database Technology

Facilities Manager Local Device Tracking

II.1 Running a Crystal Report from Infoview

Flask Web Development Course Catalog

Pro ASP.NET MVC 2 Framework

Beginning ASP.NET. 4.5 in C# Matthew MacDonald

Need to Node: Profiling Node.js Applications

Topic 15: Authentication

"Charting the Course... Comprehensive Angular 5. Course Summary

Introduction to Java. Handout-3a. cs402 - Spring

1 CS480W Quiz 6 Solution

Dreamweaver CS5 Lab 4: Sprys

Contents Introduction 1

Getting started with Convertigo Mobilizer

Kentico CMS 6.0 User s Guide

AWS Lambda Functions 9/22/15 & 9/24/15 CS 6030 Tyler Bayne

Introduction... xv SECTION 1: DEVELOPING DESKTOP APPLICATIONS USING JAVA Chapter 1: Getting Started with Java... 1

DB2 Web Query (REST based) Application Extension. Usage Instructions

CGS 3066: Spring 2015 JavaScript Reference

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

A Web-Based Introduction

This is CS50. Harvard College Fall Quiz 1 Answer Key

JavaScript: The Definitive Guide

CSE 154 LECTURE 13: SESSIONS

HarePoint HelpDesk for SharePoint Administration Guide

Lecture 8. ReactJS 1 / 24

Introduction to PTC Windchill ProjectLink 11.0

Installation & Configuration Guide Enterprise/Unlimited Edition

Mike Cantelon Marc Harter T.J. Holowaychuk Nathan Rajlich. FOREWORD BY Isaac Z. Schlueter MANNING

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

Telerik Corp. Test Studio Standalone & Visual Studio Plug-In Quick-Start Guide

What is Node.js? Tim Davis Director, The Turtle Partnership Ltd

Transcription:

COMP 2406: Fundamentals of Web Applications Fall 2013 Mid-Term Exam Solutions 1. ( false ) HTTP cookies are only sent to a web server when explicitly requested. 2. ( false ) Cookies are normally parsed by client-side JavaScript. 3. ( true ) Arbitrary (even undefined) properties of objects can be accessed in JavaScript without generating a runtime error. 4. ( true ) var causes a symbol to be defined within the inner most function context. 5. ( false ) Write access to object properties are resolved by looking at the object s properties and then by looking at the chain of objects pointed to by the objects on the prototype chain. 6. ( true ) The apply function invocation pattern allows you to set the value of this. 7. ( true ) In general, HTML forms can be submitted successfully even if the browser has disabled JavaScript. 8. What HTTP command is normally used to request the contents of a page? A: GET 9. How could I modify this application so that the player s name is displayed on every room page rather than player? A: Change line 7 of room.jade to have #{player} instead of player 10. (2) In the http module, createserver() should be originally assigned as a method of what object? Why? A: It should have been assigned as a method of the exports object, because it is the exports object that is returned when a module is require d. 11. (2) What port will this application listen on? Explain briefly how you know. A: It will listen on the port specified by the PORT environment variable or on port 3000 if it is not defined. We know this because that is the value set for the port property in line 34. 12. (15) How will program functionality change or what sort of error do you expect to see when the following lines are deleted from this application? i. app.js, lines 1-2 A: The symbols express, http, and app will no longer refer to objects (and will be undefined), and so a reference error will be generated the first time one of their methods is accessed, which is on line 34. ii. app.js, line 4 A: The symbols i and theroom, and loggedinusers will no longer be defined in the app.js file scope and loggedinusers will no longer be set to the empty object. No errors will be produced, however, as the loop on 45 48 will still work, with i and theroom just being defined in the global scope. loggedinusers not being defined won t make any difference as it is not referenced anywhere in the program. iii. app.js, line 6 A: Removing this line will mean the Engineering room is no longer defined. This will first cause problems when line 28 is called in response to the user logging in and attempting going to the Engineering room, as contents will be undefined. iv. app.js, line 11 A: The server will return no response when / is requested and the user is logged in. 1

v. app.js, line 17 A: The user will never be logged in because the player property of the session object will never be set. Thus after logging in the engineering page will immediately redirect to / to have the user try to log in again. vi. app.js, line 21 A: The user can never log out because the session will never be destroyed. Instead on quit it will redirect to / which will then redirect to the room they were previously in. vii. app.js, line 35 A: No templates will be able to be found for jade, so the first render call, for / most likely, will generate some sort of error. viii. app.js, line 37 A: No HTTP requests will be logged to the console. ix. app.js, line 38 A: The body of HTTP POST requests won t be parsed, meaning req.body will not be defined. This will cause a reference error the moment the user tries to log in (line 17). x. app.js, line 46 A: theroom will not be defined (will give a value undefined. In most programming languages this means we d get an error the moment we try to append / onto it on line 47. However, this is JavaScript, so instead we ll get the string /undefined. As it turns out, the program will run fine until the user logs in. At that point it will return an error that you cannot get /engineering (because we only defined handlers for /undefined). xi. index.jade, line 4 A: The initial page, /, won t have a title shown in the browser s title bar. The rendered page will look the same otherwise. xii. index.jade, line 12 A: There won t be a place to enter the player s name, and so the form will get submitted with an empty form value. This will cause an error on the server when it tries to access req.body.player in start(). (Yes, you should always check whether data coming from a client is correct and complete. This is an example of why.) xiii. room.jade, lines 10-12 A: No exits will be listed for every room. xiv. room.jade, lines 13-14 A: There will be no quit button on every room and thus no way to quit back to the login screen (except for restarting your browser). xv. package.json, line 9 A: Express won t be installed in node modules by npm, meaning the application will generate an error on the line where express in required (line 1 of app.js). 13. (4) List all functions including methods that are being invoked (directly called) on lines 50-51 and explain very briefly what each does. (Do not include any functions that are just being defined, are being called implicitly, or will be called during a later function invocation.) A: The only functions that are being called on these lines are 1) createserver(), a method of http that creates and returns a server object, 2).listen() which is a method of the returned server object that starts the web server listening on a given port, and 3) app.get() which returns the port to listen on. Note the anonymous function and all of the calls inside of it are called by.listen() after the server has started running. 14. (4) Jade code can include JavaScript code. What user-defined symbols can this code access? In other words, in what JavaScript scope does Jade code run in? 2

A: The only user-defined symbols Jade code can access are those symbols passed as properties of the object passed as the second parameter of the res.render() call that invokes the Jade template. Thus Jade runs in a scope that is isolated from the rest of the program (it is not nested in any other lexical context). 15. (4) What function is called to handle /engineering? How does this function get the information needed to populate the room.jade template? Explain. A: The handler() function returned by makeroomhandler() handles /engineering. It gets the necessary information from the parameters passed to makeroomhandler() in order to define a handler for /engineering. Note these parameters are retained even though makeroomhandler() has exited because the returned instance of handler() can still access them. 16. (10) Outline the control flow of this application, starting from when a user requests / from the application in a browser. Outline what the user sees and what happens in response to each input. Include what happens when the user logs in, navigates between rooms, etc. Be sure to cover each HTTP request (GET or POST) and each HTML page that is sent in response. Assume no errors. A: The user s browser sends a GET request for / index() processes the request, returns the initial login page (in HTML, rendered from the index.jade template) to the browser in response to the GET. The user enters their name and clicks the Start button. This generates a POST request for /start. start() processes the incoming post request and returns a redirect request to /engineering. The browser processes this redirect and submits a GET request for /engineering handler() instantiated for handling /engineering handles the request, returning the engineering page as defined by the room.jade template. If the user chooses to exit the room to sickbay, the click on the sickbay link generates a GET request for /sickbay. The response is handled similarly to that for /engineering. The user can move freely between sickbay and engineering by clicking on the only exit link. Each request generates a GET request to the server for the appropriate room which is then handled by an instance of handler(). If the user clicks the Quit button, it generates a POST request for /quit. This is handled on the server by quit() which destroys the session and returns a redirect response to / to the browser. The browser then sends a GET request for /. app.js: 1 var express = require( express ), http = require( http ), 2 app = express(); 3 4 var i, theroom, loggedinusers = {}; 5 var rooms = { activerooms: [ sickbay, engineering ], 6 engineering: { title: "Engineering", roomexits: [ sickbay ] }, 7 sickbay: { title: "Sickbay", roomexits: [ engineering ] } }; 8 9 function index(req, res) { 10 if (req.session.player) { 11 res.redirect("/" + req.session.currentroom); 12 } else { 13 res.render( index, { title: COMP 2406 Small Adventure Demo, 14 error: req.query.error }); }} 15 3

16 function start(req, res) { 17 req.session.player = req.body.player; 18 res.redirect("/engineering") } 19 20 function quit(req, res) { 21 req.session.destroy(); 22 res.redirect("/"); } 23 24 function makeroomhandler(name, contents) { 25 handler = function(req, res) { 26 if (req.session.player) { 27 req.session.currentroom = name; 28 res.render("room.jade", {title: contents.title, 29 roomexits: contents.roomexits, 30 player: req.session.player}); 31 } else { res.redirect("/"); }} 32 return handler; } 33 34 app.set( port, process.env.port 3000); 35 app.set( views, dirname); 36 app.set( view engine, jade ); 37 app.use(express.logger( dev )); 38 app.use(express.bodyparser()); 39 app.use(express.cookieparser( COMP2406 is confusing! )); 40 app.use(express.session()); 41 42 app.get( /, index); 43 app.post("/start", start); 44 app.post("/quit", quit); 45 for (i = 0; i<rooms.activerooms.length; i++) { 46 // theroom = rooms.activerooms[i]; 47 app.get( / + theroom, 48 makeroomhandler(theroom, rooms[theroom])); } 49 50 http.createserver(app).listen(app.get( port ), function(){ 51 console.log( Express server listening on port + app.get( port )); }); index.jade: 1 doctype 5 2 html 3 head 4 title= title 5 body 6 h1= title 7 p Welcome to the #{title} 8 p Please choose your player name 9 div 10 form(action="/start", method="post") 11 div 12 input(type="text", name="player") 13 label.add-on(for="player") Player Name 14 button(type="submit") Start room.jade: 1 doctype 5 2 html 3 head 4 title= title 5 body 6 h1= title 7 p You re on a ship, player. 4

8 p Go to: 9 ul 10 each theexit in roomexits 11 li 12 a(href= theexit) #{theexit} 13 form(action="/quit", method="post") 14 button(type="submit") Quit package.json: 1 { 2 "name": "application-name", 3 "version": "0.0.1", 4 "private": true, 5 "scripts": { 6 "start": "node app.js" 7 }, 8 "dependencies": { 9 "express": "3.4.0", 10 "jade": "*" 11 } 12 } 5