-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.js
135 lines (126 loc) · 3.21 KB
/
connect.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
var mysql = require('mysql');
var settings = require('./settings');
//var con = mysql.createConnection({
// // 一定要注意 小心有炸
// host: 'localhost',
// port: 8888,
// user: 'root',
// password: '',
// database: '2017-07-02'
//});
var con = mysql.createConnection(settings.db);
var sql = {
add: 'INSERT INTO news (title,content,imageurl,newsfrom,date,tag) VALUES (?,?,?,?,?,?)',
ask: `SELECT * FROM news`,
upd: `UPDATE news SET title = ? , content = ? , imageurl = ? , newsfrom = ? , date = ? , tag = ? WHERE Id = ?`,
del: 'DELETE FROM news ',
pag: `SELECT COUNT(id) FROM news`
};
con.add = function(sql1) {
let sql2 = sql1 || "";
function test(req, res, err) {
var obj = req.body;
console.log(req.body);
con.query(sql.add + sql2, [obj.title, obj.content, obj.imageurl, obj.newsfrom, obj.date, obj.tag], function(err1, results, fields) {
if(err1) throw err1;
else {
console.log(results);
res.send({
id: results.insertId,
error: 0
});
}
// con.end();
});
// con.end();
};
return test;
}
con.ask = function(sql1) {
let sql2 = sql1 || "";
function test(req, res, err) {
if(sql1 === 'tag')
sql2 = " WHERE `tag`='" + req.body.tag + "'" + " ORDER BY date ";
// console.log(req.url); //ask
// console.log(req.baseUrl); //admin2
// console.log(req.path);//ask
// data.unshift(obj);
// let str = JSON.stringify(data, null, '\t');
con.query(sql.ask + sql2, function(err, results, f) {
// console.log(results);
// console.log(sql.ask + sql2);
// res.send({state:'ok',data:results[0]})
res.send(results);
// con.end();
});
};
return test;
// con.end();
}
con.del = function(sql1, id1) {
let sql2 = sql1 || "WHERE id = ? ";
function test(req, res, err) {
let obj = req.body; //obj 请求的个体
let id2 = id1 || [obj.id];
console.log(sql.del + sql2);
con.query(sql.del + sql2, id2, function(err1, results, fields) {
if(err1) throw err1;
else {
// res.send({
// error: 0
// });
res.send("111222111");
}
})
// con.end();
};
return test;
}
con.upd = function(sql1) {
let sql2 = sql1 || "";
function test(req, res, err) {
var obj = req.body;
con.query(
sql.upd + sql2, [obj.title, obj.content, obj.imageurl, obj.newsfrom, obj.date, obj.tag, obj.id],
function(err1, results, fields) {
// console.log("!!!");console.log(err1);
// console.log("!!!");console.log(results);
// console.log("!!!");console.log(fields);
if(err1) throw err1;
else {
res.send({
error: 0
});
}
// con.end();
});
}
return test;
}
//未完成
con.pag = function(sql1) {
let sql2 = sql1 || "";
function test(req, res, err) {
if(sql1 === 'tag')
sql2 = " WHERE `tag`='" + req.body.tag + "'" + " ORDER BY date ";
con.query(
sql.pag + sql2, [obj.title, obj.content, obj.imageurl, obj.newsfrom, obj.date, obj.tag, obj.id],
function(err1, results, fields) {
// console.log("!!!");console.log(err1);
// console.log("!!!");console.log(results);
// console.log("!!!");console.log(fields);
if(err1) throw err1;
else {
res.send(results);
}
// con.end();
});
}
return test;
}
//con.com =function(){
//
// function()
// return test;
//}
module.exports = con;