Given a server.js file for an express server with the following code: var express = require(express);...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Given a server.js file for an express server with the following code: var express = require("express"); var app = express(); var path = require("path"); // Your code goes here: II... . app.listen(8080); Save Answer Write code to respond to the routes or complete the tasks based on the following descriptions. All callback functions must be written in the new ES6 "Arrow Function" syntax. Part 1: Define a route which matches the method "ET" and path "/request" and returns a JSON object which contains all properties with values of the http request headers. [2 marks] Waqar Ahmad Part 2: Define a route which matches the method "GET and path employees" and returns a JSON formatted string: {"message": "All employees"}. Additionally, this route will support optional queries: [3 marks] • "/employees?department-HR" which returns a JSON formatted string with the value of department: {"message": "HR"} "/employees?type=PERMANENT" which returns a JSON formatted string with the value of type: {"message": "PERMANENT" } Part 3: Define a route which matches the method "GET" and path "/employee/employeeID" and renders the text in a pair of div tag: "The parameter value: 'employeeID"" where the 'employeeID' is the parameter value, e.g. '578066058', passed in the url. [2 marks] Part 4: Define a route which matches the "route" that is invoked if no other paths are matched (you may assume that it is located below the other route definitions) and sends text "page not found" to the user. Ensure that a 404 status code is send back. [2 Marks] Part 5: Assuming a folder named "static" has been created with an image file web322.png in it. The 'static' folder is located at the above server's base folder and has been set up as a static folder. Write the full url for accessing web322.png using browser if the server starts in your Visual Studio Code. [1 Mark] NOTE: You must label/comment your code with "//Part 1", "//Part 2", "//Part 3", "//Part 4", "//Part 5", otherwise mark deduction will be applied. For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac). Question 5 To get or check the value of the response (i.e. res) header named "content-type' in Express.js, use the code: Create a JavaScript function named randomMark. In the function, a random mark from 0 to 100 will be generated by using Math.floor(Math.random()*101). The function returns a new "Promise" that will "reject" with message "the mark is unexpected." if the mark if below 60 and "resolve" with the random mark otherwise. Then call the function to output the "resolved" mark or the "rejected" message using console.log(). Question 1 Part A: Create a JavaScript function named getMy SquareRoot that takes a number parameter (e.g. x) and returns the value of the expression √x-2 or Math.sqrt(x-2). [2.5 marks] If the parameter is not a number, throw a new error with the message "Error: the parameter is invalid."; • if the parameter's value is less than 2, throw a new error with the message "Error: the parameter's value must be greater than or equal to 2." 5 points ✓ Save Part B: Generate a random integer using "Math.floor(Math.random()*20)". Write code to call the getMy SquareRoot function and pass the random number as parameter. Then use console.log() to output the result and error message. [2.5 marks] For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac). Question 4 When using function constructor to create a JavaScript object, which keyword or modifier should be used to initialize property values: O var O const O this Olet A Moving to another question will save this response. Question 5 To declare a block-scoped local variable, you use the keyword: O var o let O this O const A Moving to another question will save this response. <<< Question 5 of 6 > >> 1 points ✔ Saved <<< Question 5 of 6 > >> Question Completion Status: A Moving to another question will save this response. Question 4 To create a new object with the specified prototype object and properties, use method: O Object.clone() O Object.inheritance() O Object.prototype() O Object.create() A Moving to another question will save this response. << < Question 4 of 6 > >> 1 points Saved <<< Question 4 of 6 > >> A Click Submit to complete this assessment. Question 6 To get the functions/properties defined by using the prototype property for the object named architect, use the code: architect.prototype A Click Submit to complete this assessment. Save and Submit << < Question 6 of 6 1 points ✔ Saved <<< Question 6 of 6 Save and Submit Question 3 Given a server.js file for an express server with the following code: var express = require("express"); var app = express(); var path = require("path"); // Your code goes here: // ... . 10 points app.listen (8080); Write code to respond to the routes or complete the tasks based on the following descriptions. All callback functions must be written in the new ES6 "Arrow Function" syntax. Part A: Set up the folder named "public" and located in the server's base folder to static, so the files in the static folder can be access using the server host and file name. [1 Mark] Part B: Define a route which matches the method "GET" and path "/notes" and allows the user to download the file named "week3-lecture-notes.pdf" in the server's base folder. [2 marks] Part C: Define a route which matches the method "GET" and path "/products" and returns a JSON formatted string: {"message": "All products"}. Additionally, this route will support optional queries: [3 marks] ✓ Savec "/products?origin=Canada" which returns a JSON formatted string with the value of origin : {"message": "Canada"} "/products?type-Food" which returns a JSON formatted string with value of type: {"message": "Food" } Part D: Define a route which matches the method "GET" and path "/product/productID" and renders the text in html paragraph: "The parameter value: 'productID"" where the 'productID' is the parameter value, e.g. '432066050', passed in the url. [2 marks] Part E: Define a route which matches the "route" that is invoked if no other paths are matched (you may assume that it is located below all other route definitions) and sends text "Resource not found" to the client. Ensure that a 404 status code is send back. [2 Marks] NOTE: You must label/comment your code with "//Part A", "//Part B", "//Part C", "//Part D", "//Part E", otherwise mark deduction or a zero mark will be applied. For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac). Given a server.js file for an express server with the following code: var express = require("express"); var app = express(); var path = require("path"); // Your code goes here: II... . app.listen(8080); Save Answer Write code to respond to the routes or complete the tasks based on the following descriptions. All callback functions must be written in the new ES6 "Arrow Function" syntax. Part 1: Define a route which matches the method "ET" and path "/request" and returns a JSON object which contains all properties with values of the http request headers. [2 marks] Waqar Ahmad Part 2: Define a route which matches the method "GET and path employees" and returns a JSON formatted string: {"message": "All employees"}. Additionally, this route will support optional queries: [3 marks] • "/employees?department-HR" which returns a JSON formatted string with the value of department: {"message": "HR"} "/employees?type=PERMANENT" which returns a JSON formatted string with the value of type: {"message": "PERMANENT" } Part 3: Define a route which matches the method "GET" and path "/employee/employeeID" and renders the text in a pair of div tag: "The parameter value: 'employeeID"" where the 'employeeID' is the parameter value, e.g. '578066058', passed in the url. [2 marks] Part 4: Define a route which matches the "route" that is invoked if no other paths are matched (you may assume that it is located below the other route definitions) and sends text "page not found" to the user. Ensure that a 404 status code is send back. [2 Marks] Part 5: Assuming a folder named "static" has been created with an image file web322.png in it. The 'static' folder is located at the above server's base folder and has been set up as a static folder. Write the full url for accessing web322.png using browser if the server starts in your Visual Studio Code. [1 Mark] NOTE: You must label/comment your code with "//Part 1", "//Part 2", "//Part 3", "//Part 4", "//Part 5", otherwise mark deduction will be applied. For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac). Question 5 To get or check the value of the response (i.e. res) header named "content-type' in Express.js, use the code: Create a JavaScript function named randomMark. In the function, a random mark from 0 to 100 will be generated by using Math.floor(Math.random()*101). The function returns a new "Promise" that will "reject" with message "the mark is unexpected." if the mark if below 60 and "resolve" with the random mark otherwise. Then call the function to output the "resolved" mark or the "rejected" message using console.log(). Question 1 Part A: Create a JavaScript function named getMy SquareRoot that takes a number parameter (e.g. x) and returns the value of the expression √x-2 or Math.sqrt(x-2). [2.5 marks] If the parameter is not a number, throw a new error with the message "Error: the parameter is invalid."; • if the parameter's value is less than 2, throw a new error with the message "Error: the parameter's value must be greater than or equal to 2." 5 points ✓ Save Part B: Generate a random integer using "Math.floor(Math.random()*20)". Write code to call the getMy SquareRoot function and pass the random number as parameter. Then use console.log() to output the result and error message. [2.5 marks] For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac). Question 4 When using function constructor to create a JavaScript object, which keyword or modifier should be used to initialize property values: O var O const O this Olet A Moving to another question will save this response. Question 5 To declare a block-scoped local variable, you use the keyword: O var o let O this O const A Moving to another question will save this response. <<< Question 5 of 6 > >> 1 points ✔ Saved <<< Question 5 of 6 > >> Question Completion Status: A Moving to another question will save this response. Question 4 To create a new object with the specified prototype object and properties, use method: O Object.clone() O Object.inheritance() O Object.prototype() O Object.create() A Moving to another question will save this response. << < Question 4 of 6 > >> 1 points Saved <<< Question 4 of 6 > >> A Click Submit to complete this assessment. Question 6 To get the functions/properties defined by using the prototype property for the object named architect, use the code: architect.prototype A Click Submit to complete this assessment. Save and Submit << < Question 6 of 6 1 points ✔ Saved <<< Question 6 of 6 Save and Submit Question 3 Given a server.js file for an express server with the following code: var express = require("express"); var app = express(); var path = require("path"); // Your code goes here: // ... . 10 points app.listen (8080); Write code to respond to the routes or complete the tasks based on the following descriptions. All callback functions must be written in the new ES6 "Arrow Function" syntax. Part A: Set up the folder named "public" and located in the server's base folder to static, so the files in the static folder can be access using the server host and file name. [1 Mark] Part B: Define a route which matches the method "GET" and path "/notes" and allows the user to download the file named "week3-lecture-notes.pdf" in the server's base folder. [2 marks] Part C: Define a route which matches the method "GET" and path "/products" and returns a JSON formatted string: {"message": "All products"}. Additionally, this route will support optional queries: [3 marks] ✓ Savec "/products?origin=Canada" which returns a JSON formatted string with the value of origin : {"message": "Canada"} "/products?type-Food" which returns a JSON formatted string with value of type: {"message": "Food" } Part D: Define a route which matches the method "GET" and path "/product/productID" and renders the text in html paragraph: "The parameter value: 'productID"" where the 'productID' is the parameter value, e.g. '432066050', passed in the url. [2 marks] Part E: Define a route which matches the "route" that is invoked if no other paths are matched (you may assume that it is located below all other route definitions) and sends text "Resource not found" to the client. Ensure that a 404 status code is send back. [2 Marks] NOTE: You must label/comment your code with "//Part A", "//Part B", "//Part C", "//Part D", "//Part E", otherwise mark deduction or a zero mark will be applied. For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac).
Expert Answer:
Answer rating: 100% (QA)
The required JavaScript code for ExpressJS server file serverjs is provided below with comments The ... View the full answer
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date:
Students also viewed these programming questions
-
Assignment 5: Hash Table implementation andconcordance There are three parts to this assignment. In the first two parts,you will complete the implementation of a hash map and aconcordance program. In...
-
Zanny Mouldings has the following estimated costs for the year ahead: Directly used materials $25,100 Direct labor costs $62,300 factory chief salary $37,800 Ad spend 34.000 $ Heating and lighting...
-
Arden Furniture Company, which uses a periodic inventory system, engaged in the following transactions during July of the current year: Jul. 2 Purchased inventory for cash, $6,400, less a quantity...
-
What is the accounting rate of return method of capital investment appraisal?
-
Consider the IRR and ERR measures of worth. If we define a root to mean a value for the measure that results in \(\mathrm{PW}=0\), then which of the following statements is true? a. Both IRR and ERR...
-
Koch Corporations adjusted trial balance contained the following asset accounts at December 31, 2012: Cash $7,000; Land $40,000; Patents $12,500; Accounts Receivable $90,000; Prepaid Insurance...
-
(a) Determine whether the money multiplier will increase or decrease following an increase in each of the following ratios. No explanation is required. [3 Points; 1 Point each] (i) The required...
-
Open the start file EX2019-ChallengeYourself-6-3 . The file will be renamed automatically to include your name. If the workbook opens in Protected View, click the Enable Editing button in the Message...
-
What might be the strategy to get correct the trail balance sheet and how to implement that?
-
Group Presentation The purpose of this group presentation is to examine a leadership case study issue and explore how leaders could set out to solve the problem. The presentation team will assume...
-
Describe how the student performed a payment reconciliation whilst following security procedures for cash handling ?
-
Explain to the governor of your state using the concepts of the Rothschild-Stiglitz model (but not the jargony language)how adding a mandate would affect the insurance market and who would gain or...
-
Do you think that the criticism that the international economic system and its rules are skewed to favor rich countries over poor countries is correct? Please explain why or why not
-
1. How is restricted cash handle in the balance sheet? 2. What is segregation of duties? Give an example of functions that should be separated. 3.Why do food stands have a sign that says, "If you...
-
Some stores have headquarters in other countries while outsourcing their products from cheaper countries. Because Shein is an online retailer, it has some more leeway with shipping. When Shein was...
-
What types of questions can be answered by analyzing financial statements?
-
a. Wilson filed his individual tax return on the original due date, but failed to pay $700 in taxes that were due with the return. If Wilson pays the taxes exactly 2 months late, calculate the amount...
-
Clifford Johnson has a limited partnership investment and a rental condominium. Clifford actively manages the rental condominium. During 2012, his share of the loss from the limited partnership was...
-
Ken (age 31) and Amy (age 28) Booth have brought you the following information regarding their income, expenses, and withholding for the year. They are unsure which of these items must be used to...
-
Consider a strictly increasing and strictly concave utility function \(u\) and suppose that there exist \(N\) risky assets whose returns \(\left(\tilde{r}_{1}, \ldots, \tilde{r}_{N} ight)\) admit the...
-
Given an arbitrary frontier portfolio \(w^{*}\), with \(w^{*} eq w^{\mathrm{MVP}}\), there exists a unique frontier portfolio \(w^{\mathrm{zc}}\) such that...
-
Consider a strictly increasing and strictly concave utility function \(u\) and suppose that there exist a risk free asset with return \(r_{f}>0\) and \(N\) risky assets whose returns...
Study smarter with the SolutionInn App