-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
225 lines (150 loc) · 4.76 KB
/
Copy pathserver.js
File metadata and controls
225 lines (150 loc) · 4.76 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
var express = require('express'),
path = require('path'),
bodyParser = require('body-parser'),
app = express(),
expressValidator = require('express-validator');
/*Set EJS template Engine*/
app.set('views','./views');
app.set('view engine','ejs');
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({ extended: true })); //support x-www-form-urlencoded
app.use(bodyParser.json());
app.use(expressValidator());
/*MySql connection*/
var connection = require('express-myconnection'),
mysql = require('mysql');
app.use(
connection(mysql,{
host : 'localhost',
user : 'root',
password : '',
database : 'test',
debug : false //set true if you wanna see debug logger
},'request')
);
app.get('/',function(req,res){
res.send('Welcome');
});
//RESTful route
var router = express.Router();
router.use(function(req, res, next) {
console.log(req.method, req.url);
next();
});
var curut = router.route('/cars');
//show the CRUD interface | GET
curut.get(function(req,res,next){
req.getConnection(function(err,conn){
if (err) return next("Cannot Connect");
var query = conn.query('SELECT * FROM t_user',function(err,rows){
if(err){
console.log(err);
return next("Mysql error, check your query");
}
res.render('user',{title:"RESTful Crud Example",data:rows});
});
});
});
//post data to DB | POST
curut.post(function(req,res,next){
//validation
req.assert('name','Name is required').notEmpty();
req.assert('Color','Color is required').notEmpty();
req.assert('Year','Year is required').notEmpty();
var errors = req.validationErrors();
if(errors){
res.status(422).json(errors);
return;
}
//get data
var data = {
name:req.body.name,
email:req.body.color,
password:req.body.year
};
//inserting into mysql
req.getConnection(function (err, conn){
if (err) return next("Cannot Connect");
var query = conn.query("INSERT INTO cars set ? ",data, function(err, rows){
if(err){
console.log(err);
return next("Mysql error, check your query");
}
res.sendStatus(200);
});
});
});
//now for Single route (GET,DELETE,PUT)
var curut2 = router.route('/cars/:cars_id');
curut2.all(function(req,res,next){
console.log("You need to smth about curut2 Route ? Do it here");
console.log(req.params);
next();
});
//get data to update
curut2.get(function(req,res,next){
var cars_id = req.params.cars_id;
req.getConnection(function(err,conn){
if (err) return next("Cannot Connect");
var query = conn.query("SELECT * FROM cars WHERE cars = ? ",[cars_id],function(err,rows){
if(err){
console.log(err);
return next("Mysql error, check your query");
}
if(rows.length < 1)
return res.send("cars Not found");
res.render('edit',{title:"Edit cars",data:rows});
});
});
});
//update data
curut2.put(function(req,res,next){
var cars_id = req.params.cars_id;
//validation
req.assert('name','Name is required').notEmpty();
req.assert('color','color is required').notEmpty();
req.assert('year','year is required').notEmpty();
var errors = req.validationErrors();
if(errors){
res.status(422).json(errors);
return;
}
//get data
var data = {
name:req.body.name,
color:req.body.color,
year:req.body.year
};
//inserting into mysql
req.getConnection(function (err, conn){
if (err) return next("Cannot Connect");
var query = conn.query("UPDATE cars set ? WHERE cars_id = ? ",[data,cars_id], function(err, rows){
if(err){
console.log(err);
return next("Mysql error, check your query");
}
res.sendStatus(200);
});
});
});
//delete data
curut2.delete(function(req,res,next){
var cars_id = req.params.cars_id;
req.getConnection(function (err, conn) {
if (err) return next("Cannot Connect");
var query = conn.query("DELETE FROM cars WHERE cars_id = ? ",[cars_id], function(err, rows){
if(err){
console.log(err);
return next("Mysql error, check your query");
}
res.sendStatus(200);
});
//console.log(query.sql);
});
});
//now we need to apply our router here
app.use('/api', router);
//start Server
var server = app.listen(8080,function(){
console.log("Listening to port %s",server.address().port);
});