QuestionQuestion

Assignment Background
In this assignment, you will develop a server capable of serving the order form resources from assignment #1. The server will also be responsible for tracking some sales data for each restaurant and providing that sales data in HTML format when requested. All order data that your server stores for this assignment may be stored in RAM. That is, there is no need for persistence. If your server is restarted, it is acceptable to start from an empty set of orders. The only external modules you may use for this assignment are template engine modules. For the parts that require a template engine, you may use any available template engine you desire.

Before starting your design for the assignment, you are encouraged to read through the entire specification. It is possible that later problems will inform your design decisions and, by preparing in advance, you can avoid having to significantly refactor your code as you progress through the assignment.

To begin this assignment, download the assignment2-basecode.zip file. This zip file will contain a basic solution to assignment #1, as well as a restaurants directory containing three .json files. Each of these files contains the restaurant data for one restaurant in JSON format. Note that JSON within a file can be easily read into your Node.js server and converted to a Javascript object using the require directive.

The first part of this assignment will involve serving resources like those created for assignment #1. You may use your own assignment #1 solution (recommended if it meets the requirements), start from the files provided in the zip, or write a new solution that meets the minimum requirements listed in the problem description.

Server Load
When your server loads, it must first read and store all relevant information contained in the files within the restaurants directory. Your code should be written in such a way that new files may be added to the directory containing additional restaurants' information without requiring changes to your code. Your server should not start until this initialization process has completed.

Home Page and Header
Your server must serve a home page in response to GET requests for the URL "/". At minimum, this page is required to display a basic welcome message, though you are free to add extra content. In addition, this page and all other pages within your application must display a header containing links to the following resources:

1.The home page
2.The order form
3.The restaurant stats page

Order Form
When requested (e.g., through clicking the link in the header), your server must respond with the resources required for the order form to function. As mentioned above, you can modify your own resources from assignment #1, use the provided resources, or write new resources. The requirements for the order form are listed below:

1.When the page loads, the list of restaurants used to populate the drop-downmenu must be requested from the server.
2.When a restaurant is selected from the drop-down menu or the drop-downmenu selection changes, the new restaurant data must be requested from theserver. The previously selected restaurant's data must be removed.
3.The page must provide, at minimum: a list of all menu items for the currentlyselected restaurant, the price of each of those items, a way to add items tothe order, and a summary of the order contents/price. If the page does notsupport this, there will be no way to test much of the server functionality andyour grade will reflect that.
4.When the Submit Order button is clicked, the order must be sent to the server(i.e., using an XMLHttpRequest). Your server must process the receivedorder data and update any local state required to implement the rest of theassignment requirements (e.g., updating restaurant order totals, etc.). Oncethe request has been processed successfully by the server, and a responsehas been received, the client's page must be reset to its initial state.

Restaurant Statistics Page
Page When requested (e.g., through clicking the link in the header), your server must respond with a page containing the following information for each restaurant:

1.Total number of orders received
2.Average order total (subtotal + 10% tax + delivery fee).
3.Name of the most popular item, or the name of one of the most popular ifthere is a tie. The most popular item is the one that has sold the most units(multiple units in an order should be counted multiple times).

You must use a template engine for this page. Solutions that do not make use of a template engine will receive no marks.

Code Quality and Documentation
Your code should be well-written and easy to understand. This includes providing clear documentation explaining the purpose and function of pieces of your code. You should use good variable/function names that make your code easier to read. You should do your best to avoid unnecessary computation and ensure that your code runs smoothly throughout operation. You should also include a README.txt file that explains any design decisions that you made, precise instructions detailing how to run your server, as well as any additional instructions that may be helpful.

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

const http = require('http');
const fs = require("fs");

/**
* You must use a template engine for this page. Solutions that do not make use of a
template engine will receive no marks.
*/
const pug = require("pug");

const renderHome = pug.compileFile("./views/pages/index.pug");
const renderOrder = pug.compileFile("./views/pages/order.pug");
const renderStat = pug.compileFile("./views/pages/stat.pug");

// json data of rerstaurant
var restautants = [];
/**
* Server Load (10 marks)
When your server loads, it must first read and store all relevant information contained in
the files within the restaurants directory. Your code should be written in such a way
that new files may be added to the directory containing additional restaurants'
information without requiring changes to your code. Your server should not start until
this initialization process has completed.
*/
// readdirSync prevents server to run until all file are read
fs.readdirSync('restaurants').forEach(
    function (file) {
       let rawdata = fs.readFileSync('restaurants/' + file);
       json_data = JSON.parse(rawdata);
       // push all data to restaurant
       restautants.push(json_data);
    }
);

// list of orders per restaurant
var orders = [];

// create server
const server = http.createServer(function (request, response) {

    if (request.method === "GET") {
       // home
       if (request.url === "/"
            || request.url === "/index.html") {
            let content = renderHome();
            response.statusCode = 200;
            response.setHeader("Content-Type", "text/html");
            response.end(content);
       }
       // order      
       else if (request.url === "/order"
            || request.url === "/orderform.html") {
            let content = renderOrder();
            response.statusCode = 200;
            response.setHeader("Content-Type", "text/html");
            response.end(content);
       }
       // restaurant stat
       else if (request.url === "/stat") {
            if (orders.length > 0) {
                for (const order of orders) {
                   var total = order.order_total;
                   var number = order.order_number;
                   var average = total / number;
                   order['average'] = average;
                   order.items.sort(function (a, b) {
                        return b.amount - a.amount;...
$123.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Web Development (HTML, XML, PHP, JavaScript, Adobe, Flash, etc.) Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats