-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
281 lines (202 loc) · 9.25 KB
/
server.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
var http = require('http');
var https = require('https');
var express = require('express');
var mysql = require('mysql');
var fs = require('fs');
// connect to database
var client = mysql.createConnection({
user: 'root',
password: 'q1w2e3r4',
database: 'project',
insecureAuth: true
});
// make a web server
var app = express();
app.use(express.static('public'));
app.use(express.bodyParser());
app.use(app.router);
// daum api key
var daumLocalApiKey = '15e9a80af4b72b9101d4d985c737f7e1b6308416';
var daumMapApiKey = 'e0d5fc7d17aa2f5abb974f8c0aebec4ebf66cf40';
// google apis
//var google = require('googleapis');
////var drive = google.drive('v2');
//var OAuth2Client = google.auth.OAuth2;
//
//// client id and client secret are available at
//var CLIENT_ID = '88728940115.apps.googleusercontent.com';
//var CLIENT_SECRET = '8AL1KRaVBraWCr84-vNA85EA';
//var REDIRECT_URL = 'http://songdamkr1.cafe24.com/';
//
//var oauth2Client = new OAuth2Client(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
//var fs = require('fs');
//var drive = google.drive({ version: 'v2', auth: oauth2Client });
//
//app.get('/imageTest', function (request, response) {
// drive.files.insert({
// resource: {
// title: 'testimage.png',
// mimeType: 'image/png'
// },
// media: {
// mimeType: 'image/png',
// body: fs.createReadStream('test.png') // read streams are awesome!
// }
// },function(err, response) {
//// console.log('error:', err, 'updated:', response.id);
// });
// });
//
// brand
app.get('/brand', function(request, response){
client.query('SELECT * FROM brand', function(error, data){
response.send(data);
});
});
app.get('/admin/brand', function(request, response) {
client.query('SELECT * FROM brand', function(error, data) {
response.send(data);
});
});
app.get('/brand/:id', function(request, response){
var id = Number(request.param('id'));
client.query('SELECT * FROM brand WHERE id=?', [id], function(error, data){
response.send(data);
});
});
app.post('/brand', function(request, response){
var name = request.param('name');
var image = request.files.uploadImage;
var imageSavePath = "images\/logo\/" + name + ".png";
var imagePath = "public\/" + imageSavePath;
fs.readFile(image.path, function (error, data) {
fs.writeFile(imagePath, data, function (err) {
if (err) { throw err; }
else { response.redirect("/hq.html"); }
});
});
client.query('INSERT INTO brand (name, imageurl) VALUES(?, ?)', [name, imageSavePath], function(error, data){
});
});
app.get('/admin/showItem?', function(request, response){
var id = Number(request.param('id'));
var show = Number(request.param('show'));
console.log('request id :' + id + ' show : ' + show);
client.query('UPDATE items SET clientShow=? WHERE id=?', [show, id], function(error, data){
response.send(data);
});
});
app.put('/brand/:id', function(request, response){
var id = Number(request.param('id'));
var name = request.param('name');
var imageurl = request.param('imageurl');
var query = 'UPDATE brand SET';
if (name) query += 'name="' + name + '" ';
if (imageurl) query += 'imageurl"' + imageurl + '" ';
query += 'WHERE id=' + id;
client.query('query', function(error, data){
response.send(data);
});
});
app.del('/brand/:id', function(request, response){
var id = Number(request.param('id'));
client.query('DELETE FROM brand WHERE id=?', [id], function(error, data){
response.send(data);
});
});
app.get('/data.redirect?', function(request, response) {
var lat = request.param('lat');
var lon = request.param('lon');
var name = request.param('name');
var url = 'https://apis.daum.net/local/v1/search/keyword.xml?' + 'apikey=' + daumLocalApiKey + '&location=' + lat + ',' + lon + '&query=' + name;
console.log(url);
https.get(url, function (web) {
web.on('data', function (buffer) {
response.write(buffer);
});
web.on('end', function (buffer) {
response.end();
});
});
});
// items
app.get('/items', function(request, response){
client.query('SELECT * FROM items WHERE clientShow=1', function(error, data){
response.send(data);
});
});
app.get('/admin/items', function(request, response){
client.query('SELECT * FROM items', function(error, data){
console.log('admin brand items data : ' + data.length);
response.send(data);
});
});
app.get('/admin/brandItems/:id', function(request, response){
var id = Number(request.param('id'));
client.query('SELECT I.* FROM items AS I, brand AS B WHERE I.brandId=? AND I.brandId=B.id', [id], function(error, data){
response.send(data);
});
});
app.get('/items/:id', function(request, response){
var id = Number(request.param('id'));
client.query('SELECT * FROM items WHERE id=?', [id], function(error, data){
response.send(data);
});
});
app.get('/brandItems/:id', function(request, response){
var id = Number(request.param('id'));
client.query('SELECT B.name AS brandName, I.* FROM items AS I, brand AS B WHERE I.brandId=? AND I.brandId=B.id', [id], function(error, data){
response.send(data);
});
});
app.post('/item', function(request, response){
console.log('hi');
var brandId = Number(request.param('brandId'));
console.log('brandId:'+brandId);
var name = request.param('name');
var subtitle = request.param('subtitle');
var price = Number(request.param('price'));
var detail = request.param('detail');
var image = request.files.uploadImage;
var imageSavePath = "images\/" + name + ".png";
var imagePath = "public\/" + imageSavePath;
fs.readFile(image.path, function (error, data) {
fs.writeFile(imagePath, data, function (err) {
if (err) { throw err; }
else { response.redirect("/hq.html"); }
});
});
client.query('INSERT INTO items (brandId, name, subtitle, price, detail, imageurl, clientShow) VALUES(?, ?, ?, ?, ?, ?, 0)', [brandId, name, subtitle, price, detail, imageSavePath], function(error, data){
response.send(data);
});
});
app.put('/items/:id', function(request, response){
var id = Number(request.param('id'));
var brandId = request.param('brandId');
var name = request.param('name');
var subtitle = request.param('subtitle');
var price = request.param('param');
var detail = request.param('detail');
var imageurl = request.param('imageurl');
var query = 'UPDATE items SET';
if (brandId) query += 'brandId=' + brandId + ' ';
if (name) query += 'name="' + name + '" ';
if (subtitle) query += 'subtitle="' + subtitle + '" ';
if (price) query += 'price=' + price + ' ';
if (detail) query += 'detail="' + detail + '" ';
if (imageurl) query += 'imageurl="' + imageurl + '" ';
query += 'WHERE id=' + id;
client.query('query', function(error, data){
response.send(data);
});
});
app.del('/items/:id', function(request, response){
var id = Number(request.param('id'));
client.query('DELETE FROM items WHERE id=?', [id], function(error, data){
response.send(data);
});
});
// launch a web server
http.createServer(app).listen(52273, function(){
console.log('Server Running at http://127.0.0.1:52273');
});