-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
133 lines (119 loc) · 4.41 KB
/
server.js
File metadata and controls
133 lines (119 loc) · 4.41 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
var http = require('http'),
express = require('express'),
bodyParser = require('body-parser');
var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var app = express();
app.set('views', __dirname + '/server/views');
app.set('view engine', 'jade');
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
require('./server/mysql/mysql')(app);
app.get('/api/products', function(req,res){
app.mysqlcnn.query("SELECT * from ngLOBproducts", function (err, rows) {
if(err)
{
console.log("Problem with MySQL"+err);
}
else
{
res.end(JSON.stringify(rows));
}
});
});
app.get('/api/products/:productId', function (req, res) {
/** NOTE: WE SHOULD ALWAYS TRY TO USE Stored procedure or validate string for SQL
INJECTION attacks. This is just for proof of concept purposes.
**/
if (req.params.productId == 0) {
//create new record - send back an empty record to edit
res.writeHead(200, { 'Content-Type': 'text/plain' });
var newProduct =
{ "productId": 0,
"productName": "",
"productCode": "AAA-0000", //need to match mask
"releaseDate": "2014-01-18T07:00:00.000Z", //just need valid date to pass validation
"description": "",
"cost": 0.00,
"price": 0.00,
"category": "",
"tags": "",
"imageUrl": ""
};
// { 'productId': 0 };
res.end(JSON.stringify(newProduct));
} else {
//get existing record
app.mysqlcnn.query("SELECT * from ngLOBproducts where productId =" + req.params.productId, function (err, rows, fields) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
str = '';
if (rows.length == 0) {
console.log("no such record found...");
}
else {
res.end(JSON.stringify(rows[0]));
}
});
}
});
//save or create new if productId = 0
app.post('/api/products', function (req, res) {
/** NOTE: WE SHOULD ALWAYS TRY TO USE Stored procedure or validate string for SQL
INJECTION attacks. This is just for proof of concept purposes.
**/
var sql = "";
var data = {
productId: req.body.productId,
productName: req.body.productName,
productCode: req.body.productCode,
releaseDate: req.body.releaseDate,
description: req.body.description,
cost: req.body.cost,
price: req.body.price,
category: req.body.category,
tags: req.body.tags,
imageUrl: req.body.imageUrl
};
if (data.productId > 0) {
sql = "UPDATE ngLOBproducts SET " +
"productName = '" + data.productName + "'," +
"productCode = '" + data.productCode + "'," +
"releaseDate = '" + data.releaseDate + "'," +
"description = '" + data.description + "'," +
"cost = " + data.cost + "," +
"price = " + data.price + "," +
"category = '" + data.category + "'," +
"tags = '" + data.tags + "'," +
"imageUrl = '" + data.imageUrl + "' " +
"WHERE productId = " + data.productId;
} else {
sql = "INSERT INTO ngLOBproducts( " +
"productName, productCode, releaseDate, description, cost, price, category, tags, imageUrl) " +
"VALUES ('" + data.productName + "'," +
"'" + data.productCode + "'," +
"'" + data.releaseDate + "'," +
"'" + data.description + "'," +
+ data.cost + "," +
+ data.price + "," +
"'" + data.category + "'," +
"'" + data.tags + "'," +
"'" + data.imageUrl + "')";
}
console.log('SQL: ' + sql);
app.mysqlcnn.query(sql, function (err, result) {
if (err) {
console.log("no such record found...");
}
else {
res.end();
}
});
});
app.get('*', function (req, res) {
res.render('index');
});
var port = process.env.port || 1337;
app.listen(port);
console.log('Listening on port ' + port + '...');