Two parts project (Please leave comments in the code so I can learn where the ch
ID: 3747897 • Letter: T
Question
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 namednode_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("<!DOCTYPE html><html><head><title>Cool Smiley Faces</title></head>");
response.write(encode(firstSmiley));
response.write("<p><a href="/">Back</a></p>");
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.)
unit05 lab helpers l -encode.js 1-ode modules index.jsExplanation / Answer
Part two:
To modify the index.js file (since index.js imports code from encode.js and random.js) we need to require both the file as object in our index.js file.
For that we can write two statements in the beginning of the index.js file.
var encode = require("./helpers/encode");
var random = require("./random");
After creating the object of these two files we can use the required function and data according to the requirement of what we want to do from index.js file in the project.
To use the method and properties of those two files we have to export those method and properties so that we can import it in index.js file.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.