-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
142 lines (113 loc) · 3.6 KB
/
app.js
File metadata and controls
142 lines (113 loc) · 3.6 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.set('view engine','pug');
var todos = [{id:1, title:'Task 1'}, {id:2, title:'Task 2 '}, {id:3, title:'Task 3'}];
app.use(bodyParser.urlencoded({ extended: false }))
// give permission to edit json file
app.use(bodyParser.json())
app.get('/', (request, response) => { //Fetching all the To do's on web browser
response.render('index',{message:todos,text:'The Whole List is as Follows:'});
// response.status(200).json(todos)
});
app.post('/', (request, response) => { //entering a new to do
var newTodo = request.body.title;
if(!newTodo)
return response.status(404).render('index',{text:'There was an error!Please try again'});
const new_todo={
id:todos.length+1,
title:newTodo
};
todos.push(new_todo);
response.render('index',{message:todos,text:'Modified list is as follows'});
//response.send(todos);
});
app.put('/:id',(request,response)=>{ //modifying specific postion
const mod_id=parseInt(request.params.id);
if(mod_id>todos.length)
return response.status(400).render('index',{text:'Specified index is not available'});
const mod_title=request.body.title;
todos[mod_id-1].title=mod_title;
//response.send(todos);
response.render('index',{message:todos,text:'Modified list is as follows'});
});
app.post('/:id',(request,response) =>{ //Posting in an specific position
const mod_id=request.params.id;
if(mod_id>todos.length)
return response.status(400).render('index',{text:'Specified index is not available'});
else{
var todos_mod=[];
flag=false;
visited=false;
for(i=0;i<=todos.length+1;i++)
{
element=todos[i];
if(element==undefined)
{
todos_mod.push({
id:i+1,title:temp
});
todos=todos_mod;
break;
}
if(element.id==mod_id){
flag=true;
}
if(flag==false){
todos_mod.push(element);
continue;
}
if(flag==true)
{
if(visited==false){
visited=true;
temp=element.title;//current
element.title=request.body.title;
element.id=i+1;
todos_mod.push(element);
}
else
{
temp1=element.title;
element.title=temp;
element.id=i+1;
todos_mod.push(element);
temp=temp1;
}
}
}
}
response.render('index',{message:todos,text:'Modified list as follows'});
//response.send(todos_mod);
});
app.get('/:id',(request,response)=>{
const pos=parseInt(request.params.id);
if(pos>todos.length){
response.status(404).render('index',{text:'Error....Please enter a valid id'});
}
else
response.render('index',{message:todos,id:pos,text:'Item at that position is:'});
//response.send(todos[pos-1]);
});
app.delete('/:id',(request,response) =>{
var del_id=parseInt(request.params.id);
if(!del_id)
response.status(400).render('index',{text:'There is an error!...Please Enter valid data '});
if(del_id<=todos.length){
todos.splice(del_id-1,1);
i=1;
todos.forEach(element => {
element.id=i;
i++;
});
}
else
return response.status(404).render('index',{text:'The content with specified id is not found'});
//response.send(todos);
response.render('index',{message:todos,text:'After deleting,list is:'});
});
const port=process.env.PORT ||3000;
app.listen(port,()=>{
console.log("Listening on port "+port);
console.log('http://localhost:'+port);
});