-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
130 lines (122 loc) · 4.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// first step after creating index.js is to install npm for this use terminal and run command npm init
// Second step is in package.json
// second step is to add this start function in this scripts section and use "nodemon index.js",
// third step is to create the below two lines http and port
// const http=require('http');
const port= 1500;
const express= require('express');
const path=require('path');
const app=express();
const db=require('./config/mongoose');
const Task=require('./models/task');
app.use(express.urlencoded());
app.use(express.static('assets'));
var taskPending=[]
app.set('view engine','ejs');
app.set('views',path.join(__dirname,'views'));
// const fs=require('fs');
app.get('/',async function(req,res){
try{
const task=await Task.find({});
const pendingCount = task.filter(task => !task.checkbox).length;
const completedCount = task.filter(task => task.checkbox).length;
return res.render('index', {
title: "TODO TASK APP",
task_list:task,
pendingCount,
completedCount
});
}catch(err){
console.log(`Error in fetching contact from db ${err}`);
return
}
});
app.post('/create-task',async function(req,res){
// taskPending.push({
// taskName:req.body.taskName,
// category:req.body.category,
// date: req.body.date,
// checkbox:false
// })
try{
const newTask=await Task.create({
taskName:req.body.taskName,
category:req.body.category,
date:req.body.date,
checkbox:false
});
console.log('Task Created',newTask);
return res.redirect('/');
}catch(err){
console.log(`error in creating a task: ${err}`)
return;
}
});
app.get('/usermanual',function(req,res){
return res.render('usermanual',{title:"How-to-Use"});
});
app.get('/edit-task', async function (req, res) {
const taskName = req.query.taskName;
try {
const task = await Task.findOne({ taskName });
if (!task) {
return res.status(404).json({ message: 'Task not found' });
}
// Toggle the checkbox value
task.checkbox = !task.checkbox;
await task.save();
return res.status(200).json({ message: 'Task updated successfully' });
} catch (err) {
console.error(`Error updating task: ${err}`);
return res.status(500).json({ message: 'Internal Server Error' });
}
});
app.post('/delete-tasks', async function (req, res) {
try {
// Delete tasks where checkbox is true
await Task.deleteMany({ checkbox: true });
return res.redirect('/');
} catch (err) {
console.error(`Error deleting tasks: ${err}`);
return res.status(500).json({ message: 'Internal Server Error' });
}
});
app.listen(port,function(err){
if (err){
console.log(`Error in running the server on port number ${port}`)
}console.log(`Server is running on port ${port}`)
});
// 5th step is to create the below function
// function requestHandler(req,res){
// console.log(req.url);
// res.writeHead(200,{'content-type':'text/html'});
// // fs.readFile('./index.html',function(err,data){
// // if (err){
// // console.log(`Error occured while completing your request: ${err}`);
// // return res.end(404);
// // } return res.end(data);
// // });
// let filepath;
// switch(req.url){
// case '/':
// filepath='./index.html'
// break;
// default:
// filepath='./404.html'
// break;
// };
// fs.readFile(filepath,function(err,data){
// if (err){
// console.log(`Error occured while loading the page: ${err}`);
// return res.end(`<h1>Error</h1>`);
// }return res.end(data);
// });
// };
// // 4th step is to create a server with create server function of http and store it in const server and create a sever.listen function to connect with the port
// const server=http.createServer(requestHandler);//added the function requestHandler after creating it before this the brackets were empty
// server.listen(port,function(err){
// if (err){
// console.log("Error in running the server");
// return;
// }console.log(`Server is running on: ${port}`);
// });