-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (73 loc) · 2.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
const express= require("express")
const mongoose=require("mongoose")
const app= express();
const PORT= 8018;
app.use(express.json());
const usermodel = require("./models/usermodel");
mongoose.connect('<database connect link>')
app.listen( PORT, ()=>console.log(`new with git repo at http://localhost:${PORT}`)
)
app.get("/regions", (req,res)=>{
res.status(200).
send({'select':'please select preffered zone as your aws region',
'options':'USA, INDIA, EU, AFRICA, JAPAN,NORWAY' }
)
})
app.post('/regions/:zone', async (req,res)=>{
const {zone}=req.params;
const {server}=req.body;
const {name}=req.body;
const id=Math.round(Math.floor(Math.random()*100));
if(!server){
res.status(418).send('enter the server')
}
await usermodel.create({
name:`${name}`,
id:`${id}`,
server:`${server}`,
zone:`${zone}`
})
res.json({
"result": `${name} your zone is ${zone} with id ${id} and server ${server}`
})
})
app.get('/regions/server', async (req,res)=> {
//it will show all the param database collections on get method address.
usermodel.find({}).then(
function(param){
res.json(param)
}).catch(function(err){
console.log(err);
})
//directly create through function when these get method will be called and will be seen in database
/*async function insert(){
await param.create({
id:1,
server:800,
zone:'russia'
name:'rambo'
})
}
insert()*/
//it will show the param database collections in console log.
/*usermodel.find({}).then(
function(param){
console.log(param)
}).catch(function(err){
console.log(err);
})*/
});
//these method is gone called and the function will be requested to find and update the server.
app.post('/regions/zone/servch',async (req,res)=>{
const {name}=req.body;
const server= Math.round(Math.floor(Math.random()*10));
if(!name){
res.json("please enter the name")
}
await usermodel.findOneAndUpdate({name:`${name}`},{server:`${server}`}).then(
function(param){
res.json(param)
}).catch(function(err){
console.log(err);
})
})