Answered step by step
Verified Expert Solution
Question
1 Approved Answer
var express = require ( ' express ' ) ; var app = express ( ) ; var cors = require ( ' cors '
var express requireexpress;
var app express;
var cors requirecors;
var bodyParser requirebodyparser";
app.usecors;
app.usebodyParserjson;
let todoList ;
let completedList ;
app.useexpressstaticdirname;
app.gettasks function req res
console.logGET tasks;
res.statusjson
todo: todoList,
completed: completedList
;
;
app.postaddTask function req res
const task req.body.task;
console.logPOST addTask task;
if task todoList.includestask completedList.includestask
return res.statussendBad Request: task already exists!;
todoList.pushtask;
res.statussendTask added successfully';
;
app.postcompleteTask function req res
const task req.body.task;
console.logPOST completeTask task;
const taskIndex todoList.indexOftask;
if taskIndex
return res.statussendBad Request: task not found in TODO list!;
todoList.splicetaskIndex;
completedList.pushtask;
res.statussendTask completed successfully';
;
app.postdeleteTask function req res
const task req.body.task;
const taskIndex completedList.indexOftask;
console.logPOST deleteTask task;
if taskIndex
return res.statussendBad Request: task not found in Completed list!;
completedList.splicetaskIndex;
res.statussendTask deleted successfully';
;
app.postclearAll function req res
console.logPOST clearAll;
todoList ;
completedList ;
res.statussendAll tasks cleared';
;
const PORT ;
app.listenPORT function
console.logServer is running on http:localhost:$PORT;
;
I keep getting this code ; Uncaught ReferenceError: require is not defined
at app.js:: I already did npm install express and it doesnt work ; I have read that wen browser do not accept require. How can i fix this error var express requireexpress;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started