Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

var express = require ( ' express ' ) ; var app = express ( ) ; var cors = require ( ' cors '

var express = require('express');
var app = express();
var cors = require('cors');
var bodyParser = require("body-parser");
app.use(cors());
app.use(bodyParser.json());
let todoList =[];
let completedList =[];
app.use(express.static(__dirname));
app.get('/tasks', function (req, res){
console.log('GET /tasks');
res.status(200).json({
todo: todoList,
completed: completedList
});
});
app.post('/addTask', function (req, res){
const task = req.body.task;
console.log('POST /addTask', task);
if (!task || todoList.includes(task)|| completedList.includes(task)){
return res.status(400).send('Bad Request: task already exists!');
}
todoList.push(task);
res.status(201).send('Task added successfully');
});
app.post('/completeTask', function (req, res){
const task = req.body.task;
console.log('POST /completeTask', task);
const taskIndex = todoList.indexOf(task);
if (taskIndex ===-1){
return res.status(400).send('Bad Request: task not found in TODO list!');
}
todoList.splice(taskIndex,1);
completedList.push(task);
res.status(200).send('Task completed successfully');
});
app.post('/deleteTask', function (req, res){
const task = req.body.task;
const taskIndex = completedList.indexOf(task);
console.log('POST /deleteTask', task);
if (taskIndex ===-1){
return res.status(400).send('Bad Request: task not found in Completed list!');
}
completedList.splice(taskIndex,1);
res.status(200).send('Task deleted successfully');
});
app.post('/clearAll', function (req, res){
console.log('POST /clearAll');
todoList =[];
completedList =[];
res.status(200).send('All tasks cleared');
});
const PORT =3000;
app.listen(PORT, function(){
console.log(`Server is running on http://localhost:${PORT}`);
});
I keep getting this code ; Uncaught ReferenceError: require is not defined
at app.js:1:15 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 = require('express');

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions

Question

BPR always involves automation. Group of answer choices True False

Answered: 1 week ago