-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoi.js
78 lines (61 loc) · 1.66 KB
/
joi.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
const Joi = require('joi');
const express = require('express');
const app=express();
app.use(express.json());
const data =[
{id:1,name:"swapnil", Email:"swapnilshinde2000@gmail.com"},
{id:2,name:"swapnilCollage", Email:"swapnil.satish17@siesgst.ac.in"}
]
const port = process.env.PORT || 5000;
app.listen(port,()=>{
console.log(`Listning on port ${port}...!`);
});
app.post('/NodeJS',(req,res)=> {
test={
name: Joi.string().required().min(3)
};
const newdata={
id: data.length+1,
name:req.body.name,
//Email:req.body.Email
};
//const result =validateName(newdata.name);
const result=Joi.validate(this.newdata,test);
if(result.error) return res.send(error.details[0].message);
data.push(newdata);
res.send(data);
})
app.get('/NodeJS',(req,res)=>{
res.send(data);
});
app.get('/NodeJS/:id',(req,res)=>{
const Course=data.find(c=>c.id===parseInt(req.params.id));
if(!Course){
res.status(404).send("Course Id not found");
return;
}
res.send(Course);
});
app.put('/NodeJS/:id',(req,res)=>{
const Course=data.find(c=>c.id===parseInt(req.params.id));
if(!Course){
res.status(404).send("Course Id not found");
return;
}
newdata={
name:req.body.name
};
test={
name: Joi.string().required().min(3)
} ;
const {error}=Joi.validate(req.body.name,test);
if(error) return res.send(error.details[0].message);
Course.name=newdata.name;
res.send(data);
})
/*function validateName(val){
test={
name: Joi.string().required().min(3)
}
return Joi.validate(val,test);
}*/