-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
314 lines (252 loc) · 10.9 KB
/
server.js
File metadata and controls
314 lines (252 loc) · 10.9 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Setup core variables for the RESTful application and database connection
var node_port = 3030;
var express = require('express'); // This makes REST easy
var app = express();
var db = require('mysql'); //This sets up the MySQL connection
var db_pool = db.createPool({
host : 'localhost',
port : '',
database : 'node_js_sample',
user : 'root',
password : ''
});
/* Setup static page delivery */
app.configure( function (){
app.use( express.static( __dirname + '/public' ) ); // This is the client side!
app.use( express.logger( 'dev' ) ); // Any request to the server will print out in the console bash shell
app.use(express.bodyParser());
});
/**
* All active list
*/
app.get( '/api/db/list', function ( objRequest, objResponse ){
// Set response type from text/html to application/json
objResponse.setHeader( 'content-type', 'application/json' );
// Get a connection to the database
db_pool.getConnection( function ( objError, objConnection ){
// check for any connection errors
if( objError ){
// There was an error, so send JSON with an error message and an HTTP status of 503 (Service Unavailable)
sendError( objResponse, 503, 'error', 'connection', objError );
}else{
objConnection.query(
'SELECT * FROM list',
function ( objError, objRows, objFields ){
if( objError ){
sendError( objResponse, 500, 'error', 'query', objError );
}else{
objResponse.send({
result : 'success',
//err : '',
//err_type : '',
//fields : objFields,
rows : objRows,
length : objRows.length
});
}
}
);
objConnection.release();
}
});
});
/**
* All active list with ID
*/
app.get( '/api/db/list/id/:id', function ( objRequest, objResponse ){
// Set response type from text/html to application/json
var id = objRequest.params.id;
objResponse.setHeader( 'content-type', 'application/json' );
// Get a connection to the database
db_pool.getConnection( function ( objError, objConnection ){
// check for any connection errors
if( objError ){
// There was an error, so send JSON with an error message and an HTTP status of 503 (Service Unavailable)
sendError( objResponse, 503, 'error', 'connection', objError );
}else{
objConnection.query(
'SELECT * FROM list where id = '+id+'',
function ( objError, objRows, objFields ){
if( objError ){
// Couldn't get the query to run, so send JSON with an error message and an HTTP status of 500 (Internal Server Error)
sendError( objResponse, 500, 'error', 'query', objError );
}else{
// We have query results back, so lets put the results in JSON and return them
objResponse.send({
result : 'success',
//err : '',
//err_type : '',
//fields : objFields,
rows : objRows,
length : objRows.length
});
}
}
);
objConnection.release();
}
});
});
/**
* All active list with ID
*/
app.post( '/api/db/list/oneRecord', function ( objRequest, objResponse ){
// Set response type from text/html to application/json
//var id = objRequest.params.id;
var id = objRequest.body.id;
objResponse.setHeader( 'content-type', 'application/json' );
// Get a connection to the database
db_pool.getConnection( function ( objError, objConnection ){
// check for any connection errors
if( objError ){
// There was an error, so send JSON with an error message and an HTTP status of 503 (Service Unavailable)
sendError( objResponse, 503, 'error', 'connection', objError );
}else{
objConnection.query(
'SELECT * FROM list where id = '+id+' ',
function ( objError, objRows, objFields ){
if( objError ){
// Couldn't get the query to run, so send JSON with an error message and an HTTP status of 500 (Internal Server Error)
sendError( objResponse, 500, 'error', 'query', objError );
}else{
// We have query results back, so lets put the results in JSON and return them
objResponse.send({
result : 'success',
//err : '',
//err_type : '',
//fields : objFields,
rows : objRows,
length : objRows.length
});
}
}
);
objConnection.release();
}
});
});
/*
* Inserting data in MySQL DB
*/
app.post( '/api/db/list/insertRecord', function ( objRequest, objResponse ){
// Set response type from text/html to application/json
//var id = objRequest.params.id;
var name = objRequest.body.name;
objResponse.setHeader( 'content-type', 'application/json' );
// Get a connection to the database
db_pool.getConnection( function ( objError, objConnection ){
// check for any connection errors
if( objError ){
// There was an error, so send JSON with an error message and an HTTP status of 503 (Service Unavailable)
sendError( objResponse, 503, 'error', 'connection', objError );
}else{
//console.log('INSERT INTO list ("name") VALUES ("'+name+'");');
objConnection.query(
'INSERT INTO list (id, name) VALUES (NULL,"'+name+'") ',
function ( objError, objRows, objFields ){
if( objError ){
// Couldn't get the query to run, so send JSON with an error message and an HTTP status of 500 (Internal Server Error)
sendError( objResponse, 500, 'error', 'query', objError );
}else{
// We have query results back, so lets put the results in JSON and return them
objResponse.send({
result : 'success',
//err : '',
//err_type : '',
//fields : objFields,
rows : objRows,
length : objRows.length
});
}
}
);
objConnection.release();
}
});
});
/**
* You can fire two queries as shown below.
* here i'm taking no_view as per 'id' from DB then updating that in DB
*/
app.post( '/api/db/list/views', function ( objRequest, objResponse ){
// Set response type from text/html to application/json
//var id = objRequest.params.id;
var id = objRequest.body.id;
objResponse.setHeader( 'content-type', 'application/json' );
// Get a connection to the database
db_pool.getConnection( function ( objError, objConnection ){
// check for any connection errors
if( objError ){
// There was an error, so send JSON with an error message and an HTTP status of 503 (Service Unavailable)
sendError( objResponse, 503, 'error', 'connection', objError );
}else{
objConnection.query(
'SELECT * FROM list where id = '+id+'',
function ( objError, objRows, objFields ){
if( objError ){
// Couldn't get the query to run, so send JSON with an error message and an HTTP status of 500 (Internal Server Error)
sendError( objResponse, 500, 'error', 'query', objError );
}
else
{
//console.log(objRows);
for (var i in objRows) {
//console.log('Views : ', objRows[i].no_views);
views = objRows[i].no_views;
}
views = views+1;
//---------------------------------------------------------------------
objConnection.query(
"UPDATE `list` SET `no_views` = '"+views+"' WHERE `list`.`id` = "+id+";",
function ( objError, objRows, objFields ){
if( objError ){
// Couldn't get the query to run, so send JSON with an error message and an HTTP status of 500 (Internal Server Error)
sendError( objResponse, 500, 'error', 'query', objError );
}
else
{
// We have query results back, so lets put the results in JSON and return them
objResponse.send({
result : 'success: update views ',
views : views,
//err_type : '',
//fields : objFields,
//rows : objRows,
//length : objRows.length
});
}
}
);
//------------------------------------------------------------------------
// We have query results back, so lets put the results in JSON and return them
/* objResponse.send({
result : 'success',
//views : views,
//err_type : '',
//fields : objFields,
//rows : objRows,
//length : objRows.length
});*/
}
}
);
objConnection.release();
}
});
});
/**
* sendError is the JSON we use to send information about errors to the client-side.
* We need to check on the client-side for errors.
*/
function sendError( objResponse, iStatusCode, strResult, strType, objError ){
// I could throw errors at the HTTP response level, but I want to trap handled errors in my code instead
//objResponse.statusCode = iStatusCode;
objResponse.send({
result : strResult,
err : objError.code,
err_type : strType
});
}
/* Start listening on port 3030 */
app.listen( node_port );
console.log( "App listening on port " + node_port );