Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3. (9 marks) Write the code for the send Newltem(newitem) function in the todo.js file. This function must send the newly created item to the

image text in transcribedimage text in transcribed

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

3. (9 marks) Write the code for the send Newltem(newitem) function in the todo.js file. This function must send the newly created item to the server so the list of items may be updated. If successful, the item should be added to the client's list of items and the display must be updated. If not successful, then client-side updates do not occur. Contents of todo.html: To-Do List
Item Name:

Contents of todo.js: let items = []; function init() { document.getElementById("additem").addEventListener("click", add Item); setInterval (refreshlist, 5000); function refreshList() { req = new XMLHttpRequest(); req.onreadystatechange = function() { if(this.readyState==4 && this.status == 200){ items = JSON.parse(req. responseText).items; renderlist(); req.open("GET", "http://localhost: 3000/list'); req.send(); // Creates a new item and calls function to send the item to the server function addItem() { let itemName = document.getElementById("itemname").value; if(itemName. length == 0){ alert("You must enter an item name."); return; sendNewItem({name: itemName}); //Continues on next page... function sendNewItem(new Item) { //Your code would go here. //Removes displayed list data and renders new list using contents of items function renderList() { let list = document.getElementById("list"); while(list.firstChild){ list.removeChild(list.first Child); items.forEach(elem => { let newDiv = document.createElement("div"); let newItem = document.createElement("input"); newItem.type = "checkbox"; newItem.value = elem.name; newItem.id = elem.name; let text = document.createTextNode (elem.name); newDiv.appendChild(newItem); newDiv.appendChild(text); list.appendChild(newDiv); }); Contents of server.js: let http = require('http'); let fs = require('fs'); let path = require('path'); let items = { "items" : [{name: "study"}] }; //Helper function for sending 404 message function send484 (response) { response.writeHead ( 404, { "Content-Type': 'text/plain' }); response.end('Error 404: Resource not found.'); //Continues on next page... let server = http.createServer (function (req, res) { if (req.method == 'GET') { if (req.url == "/todo.html" || req.url == '/'){ res.writeHead (200, { "content-type": "text/html" }); fs.createReadStream("./todo.html").pipe (res); }else if (req.url == '/todo.js'){ res.writeHead(200, { "content-type': "application/javascript" }); fs.createReadStream("./todo.js").pipe (res); }else if (req.url == '/list'){ res.writeHead( 200, { "content-type': "application/json" }); res.end(JSON.stringify(items); }else{ send404(res); return; }else if (req.url == "/items" && req.method == "POST"){ res.writeHead (200, { "content-type': "text/html" }); fs.createReadStream("./todo.html").pipe(res); }else if (req.method == "POST" && req.url = '/list) let body = ""; req.on('data', (chunk) =>{ body += chunk; }); req.on('end', () => { newItem = JSON.parse(body); items. items.push(newItem); res.writeHead( 200, { "content-type':"application/json" }); res.end(); }); }else{ send404 (res); }); server.listen(3000); 3. (9 marks) Write the code for the send Newltem(newitem) function in the todo.js file. This function must send the newly created item to the server so the list of items may be updated. If successful, the item should be added to the client's list of items and the display must be updated. If not successful, then client-side updates do not occur. Contents of todo.html: To-Do List
Item Name:

Contents of todo.js: let items = []; function init() { document.getElementById("additem").addEventListener("click", add Item); setInterval (refreshlist, 5000); function refreshList() { req = new XMLHttpRequest(); req.onreadystatechange = function() { if(this.readyState==4 && this.status == 200){ items = JSON.parse(req. responseText).items; renderlist(); req.open("GET", "http://localhost: 3000/list'); req.send(); // Creates a new item and calls function to send the item to the server function addItem() { let itemName = document.getElementById("itemname").value; if(itemName. length == 0){ alert("You must enter an item name."); return; sendNewItem({name: itemName}); //Continues on next page... function sendNewItem(new Item) { //Your code would go here. //Removes displayed list data and renders new list using contents of items function renderList() { let list = document.getElementById("list"); while(list.firstChild){ list.removeChild(list.first Child); items.forEach(elem => { let newDiv = document.createElement("div"); let newItem = document.createElement("input"); newItem.type = "checkbox"; newItem.value = elem.name; newItem.id = elem.name; let text = document.createTextNode (elem.name); newDiv.appendChild(newItem); newDiv.appendChild(text); list.appendChild(newDiv); }); Contents of server.js: let http = require('http'); let fs = require('fs'); let path = require('path'); let items = { "items" : [{name: "study"}] }; //Helper function for sending 404 message function send484 (response) { response.writeHead ( 404, { "Content-Type': 'text/plain' }); response.end('Error 404: Resource not found.'); //Continues on next page... let server = http.createServer (function (req, res) { if (req.method == 'GET') { if (req.url == "/todo.html" || req.url == '/'){ res.writeHead (200, { "content-type": "text/html" }); fs.createReadStream("./todo.html").pipe (res); }else if (req.url == '/todo.js'){ res.writeHead(200, { "content-type': "application/javascript" }); fs.createReadStream("./todo.js").pipe (res); }else if (req.url == '/list'){ res.writeHead( 200, { "content-type': "application/json" }); res.end(JSON.stringify(items); }else{ send404(res); return; }else if (req.url == "/items" && req.method == "POST"){ res.writeHead (200, { "content-type': "text/html" }); fs.createReadStream("./todo.html").pipe(res); }else if (req.method == "POST" && req.url = '/list) let body = ""; req.on('data', (chunk) =>{ body += chunk; }); req.on('end', () => { newItem = JSON.parse(body); items. items.push(newItem); res.writeHead( 200, { "content-type':"application/json" }); res.end(); }); }else{ send404 (res); }); server.listen(3000)

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

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

3rd Edition

978-1119907466

More Books

Students also viewed these Databases questions

Question

Explain the nature of human resource management.

Answered: 1 week ago

Question

Write a note on Quality circles.

Answered: 1 week ago

Question

Describe how to measure the quality of work life.

Answered: 1 week ago

Question

6 What is the balanced scorecard method?

Answered: 1 week ago