-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.js
More file actions
134 lines (114 loc) · 4.34 KB
/
test.js
File metadata and controls
134 lines (114 loc) · 4.34 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
var express = require('express')
var bodyParser = require('body-parser')
var mysql = require('mysql')
var app = express()
app.use(bodyParser.json()) // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded
const SEED = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
// 创建连接池
var pool = mysql.createPool({
host: 'localhost',
user: 'root',
password: '123456',
database: 'yiea',
typeCast: function (field, next) {
if (field.type == 'BLOB' || field.type == 'TINYBLOB') {
var buff = field.buffer()
var bufferBase64 = ''
if (buff != null) {
var bufferBase64 = new Buffer(buff, 'binary').toString()
}
return bufferBase64
}
return next()
}
})
function sqlCUR (sql, params) {
// 直接使用
pool.query(sql, params, function (err, result) {
if (err) throw err
console.log('The is: ', result)
})
}
function sqlDel (sql) {
// 直接使用
pool.query(sql, function (err, rows, fields) {
if (err) throw err
// console.log('The is: ', rows)
})
}
app.get('/proxy', function (req, res) {
res.send('Hello World!')
})
app.post('/proxy/lesson', function (req, res) {
var lessonid = req.body.lessonid
var sql = 'select * from lesson where lessonid = ?'
pool.query(sql, [lessonid], function (err, result) {
if (result.length > 0) {
res.send(result[0])
}else {
res.send('{}')
}
})
})
app.get('/proxy/navi', function (req, res) {
// res.send('[{"id":"1234","title":"hello"},{"id":"4567","title":"word"},{"id":"12345","title":"hello word"},{"id":"1234","title":"hello"},{"id":"4567","title":"word"},{"id":"12345","title":"hello word"},{"id":"1234","title":"hello"},{"id":"4567","title":"word"},{"id":"12345","title":"hello word"},{"id":"1234","title":"hello"},{"id":"4567","title":"word"},{"id":"12345","title":"hello word"},{"id":"1234","title":"hello"},{"id":"4567","title":"word"},{"id":"12345","title":"hello word"},{"id":"1234","title":"hello"},{"id":"4567","title":"word"},{"id":"12345","title":"hello word"},{"id":"1234","title":"hello"},{"id":"4567","title":"word"},{"id":"12345","title":"hello word"},{"id":"1234","title":"hello"},{"id":"4567","title":"word"},{"id":"12345","title":"hello word"}]')
var sql = 'select lessonid,title from lesson order by cdate desc'
pool.query(sql, function (err, result) {
res.send(result)
})
})
app.post('/proxy/lesson/update', function (req, res) {
var sql = 'update lesson set title=? , lesson=? where lessonid=? '
var parms = [req.body.title, req.body.lesson, req.body.lessonid]
sqlCUR(sql, parms)
res.send('success')
})
app.post('/proxy/lesson/save', function (req, res) {
var timestamp = parseInt(new Date().getTime(), 16)
var lessonid = SEED[Math.floor(Math.random() * 62)] + timestamp
+ SEED[Math.floor(Math.random() * 62)]
var sql = 'insert into lesson(lessonid,title,lesson,type,cdate)'
+ 'values(?,?,?,?,?)'
var parms = [lessonid, req.body.title, req.body.lesson,
req.body.type, new Date()]
sqlCUR(sql, parms)
res.send(lessonid)
})
app.post('/proxy/word/save', function (req, res) {
var sql = 'insert into word(title,lessonid,japanese,chinese,english,example,cdate)'
+ 'values(?,?,?,?,?,?,?)'
var parms = [req.body.title, req.body.lessonid, req.body.japanese,
req.body.chinese, req.body.english, req.body.example, new Date()]
sqlCUR(sql, parms)
res.send('success')
})
app.post('/proxy/word/update', function (req, res) {
var sql = 'update word set japanese =? , chinese=? ,english=?,example=? where id=? '
var parms = [ req.body.japanese,
req.body.chinese, req.body.english, req.body.example, req.body.id]
sqlCUR(sql, parms)
res.send('success')
})
app.post('/proxy/word/delete', function (req, res) {
var sql = 'delete from word where id=? '
pool.query(sql, [req.body.id], function (err, rows, fields) {
if (err) throw err
// console.log('The is: ', rows)
})
res.send('success')
})
app.post('/proxy/word', function (req, res) {
var sql = 'select * from word where lessonid = ? order by mdate desc'
pool.query(sql, [req.body.lessonid], function (err, result) {
console.log(result);
if (result.length > 0) {
res.send(result)
}else {
res.send('[]')
}
})
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})