-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
205 lines (199 loc) · 5.08 KB
/
Copy pathserver.js
File metadata and controls
205 lines (199 loc) · 5.08 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//entire todo rest api with http
const http=require('http');
const url = require('url');
require('dotenv').config();
// In-memory array to store todo items
const todos=[
{
id:1,
text:"Get milk",
completed:false
},
{
id:2,
text:"Clean House",
completed:false
},
{
id:3,
text:"Watch dishes",
completed:true
},
{
id:4,
text:"Make rest api",
completed:false
},
]
const server=http.createServer((req,res)=>{
res.setHeader('Content-Type', 'application/json');
// GET /todos - Return all todos
if(req.method==='GET'&&req.url==='/todos'){
try {
res.end(JSON.stringify(todos))
} catch (error) {
res.end(JSON.stringify({
success:false,
error:"something went wrong"
}))
}
}
// GET /todos/:id - Return a specific todo by id
else if(req.method==='GET'){
const parsedUrl=url.parse(req.url)
//parsedUrl is an object with several things like pathname,query,.. protocol etc
//its useful in cases like "/user/123?name=Akshita"
//now split its pathname at /
const parts=parsedUrl.pathname.split('/') //first=''//second=todos //third=id
if(parts[1]!='todos'||parts.length!=3){
// Invalid URL for GET /todos/:id
res.statusCode=400;
res.end(JSON.stringify({
success:false,
error:"Invalid url"
}))
return;
}
const todo=todos.filter(todo=>todo.id===+parts[2])
if(!todo.length){
// Todo not found
res.statusCode=404;
res.end(JSON.stringify({
success:false,
error:"Todo not found"
}))
return;
}
res.end(JSON.stringify({
success:true,
data:todo
}))
}
// POST /todos - Add a new todo
else if(req.method==="POST" && req.url==='/todos'){
//getting data from req
let body=[]
req.on('data',chunk=>body.push(chunk));
let todo={};
req.on('end',()=>{
body=Buffer.concat(body).toString();
if(!body){
// No body provided
res.statusCode=400;
res.end(JSON.stringify({
success:false,
error:"Bad request: task missing"
}))
return;
}
todo=JSON.parse(body)
if(!todo.text){
// No text property in todo
res.statusCode=400;
res.end(JSON.stringify({
success:false,
error:"Bad request: task missing"
}))
return;
}
if(!todo.completed) todo.completed=false;
// Assign a new id to the todo
// todo.id=todos.length+1; //a little error here because when delete happens length decreases so we can have duplicate ids
todo.id=todos[todos.length-1].id +1;
todos.push(todo)
res.end(JSON.stringify({
success:true,
data:todo
}))
})
}
// PUT /todos/:id - Update an existing todo
else if(req.method==='PUT'){
const parsedUrl=url.parse(req.url);
const parts=parsedUrl.pathname.split('/');
if(parts[1]!=='todos'||parts.length!=3){
// Invalid URL for PUT /todos/:id
res.statusCode=400;
res.end(JSON.stringify({
success:false,
error:"Invalid url"
}))
return;
}
const [oldtodo]=todos.filter(todo=>todo.id===+parts[2])
if(!oldtodo){
// Todo to update not found
res.statusCode=400;
res.end(JSON.stringify({
success:false,
error:"Task doesn't exist: Try creating a new task first"
}))
return;
}
//getting data from req
let body=[]
req.on('data',chunk=>body.push(chunk));
req.on('end',()=>{
body=Buffer.concat(body).toString();
if(!body){
// No body provided
res.statusCode=400;
res.end(JSON.stringify({
success:false,
error:"Bad request: task missing"
}))
return;
}
const todo=JSON.parse(body)
if(todo.text){
oldtodo.text=todo.text
}
if(todo.completed) oldtodo.completed=todo.completed
res.end(JSON.stringify({
success:true,
data:oldtodo
}))
})
}
// DELETE /todos/:id - Delete a todo by id
else if(req.method==="DELETE"){
const parsedUrl=url.parse(req.url);
const parts=parsedUrl.pathname.split('/');
if(parts[1]!=='todos'||parts.length!=3){
// Invalid URL for DELETE /todos/:id
res.statusCode=400;
res.end(JSON.stringify({
success:false,
error:"Invalid url"
}))
return;
}
const [todo]=todos.filter(todo=>todo.id===+parts[2])
if(!todo){
// Todo to delete not found
res.statusCode=400;
res.end(JSON.stringify({
success:false,
error:"Task doesn't exist: Try creating a new task first"
}))
return;
}
todos.splice(todos.indexOf(todo),1);
res.end(JSON.stringify({
success:true,
data:{}
}))
}
// Catch-all for invalid routes or methods
else {
res.statusCode=400;
res.end(JSON.stringify({
success:false,
error:"Invalid url"
}))
}
})
// Start the server and listen on the specified port
server.listen(process.env.PORT,()=>{
console.log(`Server is listening to port ${process.env.PORT}`)
})