-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_mysql.js
199 lines (174 loc) · 5.75 KB
/
app_mysql.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var multer = require('multer');
var _storage = multer.diskStorage({
destination: function (req, file, cb) {
// if(파일의 형식이 이미지면)
// 어디에다가 넣고 cb( );
// else if (파일의 형식이 텍스트면)
// 어디에다가 넣고 cb();
cb(null, 'uploads/');
},
filename: function (req, file, cb) {
//if 만약 이미 파일이 존재한다면...등의 조건 가능
cb(null, file.originalname);
}
});
var upload = multer({
storage: _storage
});
var fs = require('fs');
// BASIC mysql connection
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'visualmedia',
database: 'o2'
});
connection.connect();
// Makes pretty html for pug files
app.locals.pretty = true;
// Using pug as an view engine
app.set('view engine', 'pug');
//Going to use this directory for views file
app.set('views', './views_mysql');
// setting to use the static page
app.use('/user', express.static('uploads/'));
// setting body-parser. Able to use body for post
app.use(bodyParser.urlencoded({
extended: false
}));
app.get('/upload', function (req, res) {
res.render('upload');
});
app.post('/upload', upload.single('userfile'), function (req, res) {
res.send('Uploaded : ' + req.file.filename);
});
app.get(['/topic', '/topic/:id'], function (req, res) {
var sql = 'SELECT id, title FROM topic';
connection.query(sql, function (err, rows, fields) {
var id = req.params.id;
if (id && (id != 'add')) {
var sql = 'SELECT * FROM topic WHERE id=?';
connection.query(sql, [id], function (err, rows_2, fields) {
if (err) {
console.log(err);
res.status(500).send('Internal Server Error');
} else {
res.render('view', {
topics: rows,
topic: rows_2[0],
});
}
})
} else if (id && (id == 'add')) {
var sql = 'SELECT id, title FROM topic';
connection.query(sql, function (err, topics, fields) {
res.render('add', {
topics: rows,
// topic:topics[0],
});
})
} else {
res.render('view', {
topics: rows,
});
}
});
});
app.get(['/topic/:id/delete'], function (req, res) {
var sql = 'SELECT id, title FROM topic';
connection.query(sql, function (err, rows, fields) {
var sql = 'SELECT * FROM topic WHERE id=?';
var id = req.params.id;
connection.query(sql, [id], function (err, topic) {
if (err) {
console.log(err);
res.status(500).send('Internal Server Error');
} else {
if (topic.length === 0) {
console.log('There is no id.');
res.status(500).send('Internal Server Error');
} else {
res.render('delete', {
topics: rows,
topic: topic[0]
})
}
}
})
})
})
app.get(['/topic/:id/edit'], function (req, res) {
var sql = 'SELECT id, title FROM topic';
connection.query(sql, function (err, rows, fields) {
if (err) {
console.log(err);
res.status(500).send('Internal Server Error');
}
var id = req.params.id;
if (id) {
var sql = 'SELECT * FROM topic WHERE id=?';
connection.query(sql, [id], function (err, rows_2, fields) {
if (err) {
console.log(err);
res.status(500).send('Internal Server Error');
} else {
res.render('edit', {
topics: rows_2,
topic: rows_2[0],
});
}
})
} else {
console.log("THERE IS NO ID");
res.status(500).send('Internal Server Error');
}
});
});
app.post('/topic/add', function (req, res) {
var sql = 'INSERT INTO topic (title, description, author) VALUES (?,?,?)';
var title = req.body.title;
var description = req.body.description;
var author = req.body.author;
connection.query(sql, [title, description, author], function (err, result, fields) {
if (err) {
console.log(err);
res.status(500).send('Internal Server Error');
} else {
res.redirect('/topic/' + result.insertId);
}
})
});
app.post('/topic/:id/edit', function (req, res) {
var sql = 'UPDATE topic SET title=?, description=?, author=? WHERE id=?';
var title = req.body.title;
var description = req.body.description;
var author = req.body.author;
var id = req.params.id;
connection.query(sql, [title, description, author, id], function (err, result, fields) {
if (err) {
console.log(err);
res.status(500).send('Internal Server Error');
} else {
res.redirect('/topic/' + id);
}
})
});
app.post('/topic/:id/delete', function (req, res) {
var id = req.params.id;
var sql = 'DELETE FROM topic WHERE id=?';
connection.query(sql, [id], function (err, result) {
if (err) {
console.log(err);
res.status(500).send('Internal Server Error');
} else {
res.redirect('/topic');
}
})
})
app.listen(3000, function () {
console.log("Connected, 3000 port :)");
});