Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Two parts project (Please leave comments in the code so I can learn where the changes has been made) Thanks Project Description: You are given

Two parts project (Please leave comments in the code so I can learn where the changes has been made) Thanks

Project Description:

You are given a Node.js project based on our Hello Node project in Unit 01 Lab Part One.

The instructor should provide you the source code in unit05_lab.zipfile in BlackBoard Unit 05 Lab sections. There are many files and folders in this project seen below:

On the top level, there are three JavaScript files (encode.js, index.js, and random.js) and a folder named node_modules. Here is a brief description of what they are:

index.js the entry point of this application, very similar to app.js in Unit 01 Lab

encode.js source file that exports a programmer defined function for HTML encoding

random.js source file that exports a programmer defined function for generating random numbers.

If you run this application now, you should see something like this:

When you click on the bottom link you will get a smiley face:

Right now you will get the same smiley face all the time. Your job is to modify this program by randomly choosing a smiley face from an array of smiley faces. You need to import the anonymous function from source file random.js.

After you have modified the program, you should get different smiley faces when you click the link. Here are some screenshots:

Hint #1:You need to import the function from random.js by using require() function. (See how encode.js is imported for clue.)

Hint #2:You need to replace the following highlighted code with some new code that will randomly pick a smiley face from the array named smileyArray.

} else if (request.url == '/smiley') {

var smileyArray = cool.faces;

var firstSmiley = smileyArray[0];

response.write("

Cool Smiley Faces");

response.write(encode(firstSmiley));

response.write("

Back

");

response.end();

You need to submit only the modified index.jsfile.

Note:

There are many free Node.js projects out there. The instructor downloaded the cool-ascii-faces from this site: https://github.com/maxogden/cool-ascii-faces.

There are three imports at the top of index.js file:

var http = require(http);

This is Node.js built-in module for handling HTTP request and response which you will need for any web applications.

var cool = require(cool-ascii-faces);

This statement imports a third-party module called cool-ascii-faces. You should put your third-party modules under a specially named folder node-modules. (Chapter 4 will explain this in detail.)

var encode = require(./encode);

This statement imports a programmer defined module (something you wrote).

________________________________________________________________

Part Two: Short answer questions

*All questions are based on the original index.js source code provided by the instructor.

Question 1: How would you modify the index.js file if the two helper JavaScript files were placed in a file structure like below? (Yellow ones are folders. Blue ones are files.)

image text in transcribed

unit05 lab s---encode.js random.js helpers node modules index.is

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

Database Processing

Authors: David M. Kroenke, David Auer

11th Edition

B003Y7CIBU, 978-0132302678

More Books

Students also viewed these Databases questions