forked from bwcho75/node.js_study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.js_mysql_advanced
128 lines (115 loc) · 3.64 KB
/
node.js_mysql_advanced
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
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var mysql = require('mysql');
var connection = mysql.createConnection({
host :'localhost',
port : 3306,
user : 'terry',
password : 'asdf1234',
database:'terry'
});
connection.connect(function(err) {
if (err) {
console.error('mysql connection error');
console.error(err);
throw err;
}
});
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
// insert
app.post('/users',function(req,res){
var user = {'userid':req.body.userid,
'name':req.body.name,
'address':req.body.address};
var query = connection.query('insert into users set ?',user,function(err,result){
if (err) {
console.error(err);
throw err;
}
console.log(query);
res.send(200,'success');
});
});
// insert with transaction
app.post('/userstx',function(req,res){
var user = {'userid':req.body.userid,
'name':req.body.name,
'address':req.body.address};
connection.beginTransaction(function(err) {
if (err) {
throw err;
}
connection.query('insert into users set ?', user, function (err, result) {
if (err) {
console.error(err);
connection.rollback(function () {
console.error('rollback error');
throw err;
});
}// if err
console.log('insert transaction log');
var log = {'userid': req.body.userid};
connection.query('insert into log set ?', log, function (err, result) {
if (err) {
console.error(err);
connection.rollback(function () {
console.error('rollback error');
throw err;
});
}// if err
connection.commit(function (err) {
if (err) {
console.error(err);
connection.rollback(function () {
console.error('rollback error');
throw err;
});
}// if err
res.send(200, 'success');
});// commit
});// insert into log
});// inset into users
}); // begin trnsaction
});
//select all
app.get('/users', function(req,res){
var query = connection.query('select * from users',function(err,rows){
console.log(rows);
res.json(rows);
});
console.log(query);
});
// select where
app.get('/users/:userid', function(req,res){
var query = connection.query('select * from users where userid='+mysql.escape(req.params.userid),function(err,rows){
console.log(rows);
res.json(rows);
});
console.log(query);
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});