-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.js
More file actions
90 lines (86 loc) · 1.84 KB
/
practice.js
File metadata and controls
90 lines (86 loc) · 1.84 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
const express=require('express')
const app=express()
const mongoose=require('mongoose')
app.use(express.json())
const port=6003
//connecting mongodb
mongoose.connect('mongodb://127.0.0.1:27017/feb20233')
.then(()=>{
console.log('connected to db')
})
.catch((err)=>{
console.log(err)
})
const Schema=mongoose.Schema()
const taskSchema=({
title:{
type:String,
required:true
},
description:{
type:String
},
createdDate:{
type:Date,
default:Date.now
},
dueDate:{
type:Date
},
completed:{
type:Boolean
}
})
//creating model
const Task=mongoose.model('Task',taskSchema)
//get all tasks
app.get('/api/tasks',(req,res)=>{
Task.find()
.then((tasks)=>{
res.json(tasks)
})
.catch((err)=>{
res.json(err)
})
})
//creating tasks
app.post('/api/tasks',(req,res)=>{
const body=req.body
const task=new Task(body)
task.save()
.then((task)=>{
res.json(task)
})
.catch((err)=>{
res.json(err)
})
})
//update
app.put('/api/tasks/:id',(req,res)=>{
const id=req.params.id
const body=req.body
Task.findByIdAndUpdate(id,body,{new:true,runValidators:true})
.then((task)=>{
res.json(task)
})
.catch((err)=>{
res.json(err)
})
})
//delete
app.delete('/api/tasks/:id',(req,res)=>{
const id=req.params.id
Task.findByIdAndDelete(id)
.then((task)=>{
res.json(task)
})
.catch((err)=>{
res.json(err)
})
})
app.get('/',(req,res)=>{
res.json('app is running')
})
app.listen(port,()=>{
console.log('server runnning on port',port)
})