-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapp.js
More file actions
64 lines (51 loc) · 1.5 KB
/
app.js
File metadata and controls
64 lines (51 loc) · 1.5 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
var express = require('express');
var path = require('path');
var mysql = require('mysql');
var db = mysql.createConnection({
host : '192.168.3.251',
user : 'sampleDB',
password : 'sampleDB',
database : 'sampleDB'
});
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.bodyParser());
app.get('/data', function(req, res){
db.query("SELECT * FROM books", function(err, rows){
if (err) console.log(err);
res.send(rows);
});
});
app.post('/data', function(req, res){
var data = req.body;
var mode = data["!nativeeditor_status"];
var sid = data.gr_id;
var tid = sid;
var sales = data.sales;
var author = data.author;
var title = data.title;
var price = data.price;
function update_response(err, result){
if (err){
console.log(err);
mode = "error";
}
else if (mode == "inserted")
tid = result.insertId;
res.setHeader("Content-Type","text/xml");
res.send("<data><action type='"+mode+"' sid='"+sid+"' tid='"+tid+"'/></data>");
}
if (mode == "updated")
db.query("UPDATE books SET sales = ?, author = ?, title = ?, price = ? WHERE id = ?",
[sales, author, title, price, sid],
update_response);
else if (mode == "inserted")
db.query("INSERT INTO books(sales, author, title, price) VALUES (?,?,?,?)",
[sales, author, title, price],
update_response);
else if (mode == "deleted")
db.query("DELETE FROM books WHERE id = ?", [sid], update_response);
else
res.send("Not supported operation");
});
app.listen(3000);