-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
155 lines (106 loc) · 4.09 KB
/
server.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
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
var express = require('express');
var app = express();
var router = express.Router();
var bodyParser = require('body-parser')
var server = require('http').createServer(app)
var io = require('socket.io')(server);
var projects = [{'id':0,'sid':'A123','name': 'Iphone','price':'200'},
{'id':1,'sid':'A124','name': 'Nokia','price':'25'},
{'id':2,'sid':'B225','name': 'Samsung','price':'150'},
{'id':3,'sid':'B226','name': 'camera','price':'250'},
{'id':4,'sid':'C327','name': 'bike','price':'120'},
{'id':5,'sid':'C328','name': 'computer','price':'320'}
]
var tmp0 = projects[0].price;
var tmp1 = projects[1].price;
var tmp2 = projects[2].price;
var tmp3 = projects[3].price;
var tmp4 = projects[4].price;
var tmp5 = projects[5].price;
app.use(express.static(__dirname+'/public'))
server.listen(8000)
console.log("Server is running")
io.sockets.on('connection', function (socket) {
// listen to incoming bids
socket.on('bid', function(content) {
// echo to the sender
console.log(content)
socket.emit('bid',{ amount : content.amount });
// broadcast the bid to all clients
socket.broadcast.emit('other bid', 'admin : ' + content.amount);
});
});
var idx = 2;
router.route('/projects')
.get(function(req, res) {
res.json(projects);
})
.post(function(req, res) {
var project = {};
project.id = idx++;
project.sid = req.body.sid
console.log(req.body.sid)
project.name = req.body.name
project.price = req.body.price
projects.push(project);
res.json({message : 'Success'})
})
router.route('/projects/:project_id')
.get (function(req,res) {
res.json(projects[req.params.project_id])
})
.put (function(req,res) {
var id = req.params.project_id
projects[id].sid = req.body.sid;
projects[id].name = req.body.name;
if(tmp0<req.body.price){
projects[id].price = req.body.price
console.log(req.body.name +' price max :'+ projects[id].price)
tmp0 = projects[id].price
}
else if(tmp1<req.body.price){
projects[id].price = req.body.price
console.log(req.body.name +' price max :'+ projects[id].price)
tmp1 = projects[id].price
}
else if(tmp2<req.body.price){
projects[id].price = req.body.price
console.log(req.body.name +' price max :'+ projects[id].price)
tmp2 = projects[id].price
}
else if(tmp3<req.body.price){
projects[id].price = req.body.price
console.log(req.body.name +' price max :'+ projects[id].price)
tmp3 = projects[id].price
}
else if(tmp4<req.body.price){
projects[id].price = req.body.price
console.log(req.body.name +' price max :'+ projects[id].price)
tmp4 = projects[id].price
}
else if(tmp5<req.body.price){
projects[id].price = req.body.price
console.log(req.body.name +' price max :'+ projects[id].price)
tmp5 = projects[id].price
}
else{
console.log(req.body.name +' Less than price')
}
res.json({ message: 'project updated!' });
})
.delete (function(req,res) {
var id = req.params.project_id
delete projects[id]
res.json({ message: 'project deleted!' });
})
router.get('/', function(req, res) {
res.json({ message: 'hooray! welcome to our api!' })
});
app.use('/api', bodyParser.json(), router);
app.use("*",function(req,res){
res.status(404).send('404 Not found');
});
/*app.listen(8000, function() {
console.log("Server is running")
});
*/