-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
71 lines (57 loc) · 1.85 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
const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');
var cors = require('cors')();
const app = express();
const port = process.env.PORT || 80;
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors);
const data = fs.readFileSync('./database.json');
const conf = JSON.parse(data);
const mysql = require('mysql');
const connection = mysql.createConnection({
host: conf.host,
user: conf.user,
password: conf.password,
port: conf.port,
database: conf.database
});
connection.connect();
const multer = require('multer');
// 사용자의 파일이 업로드 되는 폴더
const upload = multer({ dest: './upload' })
app.get('/api/gifticons', (req, res) => {
connection.query(
"SELECT * FROM GIFTICON ORDER BY (CASE WHEN used IN ('미사용') THEN 0 ELSE 1 END) , exp_date;",
(err, rows, fields) => {
res.send(rows);
}
)
});
app.use('/barcode_img', express.static('./upload'));
app.post('/api/gifticons', upload.single('barcode_img'), (req, res) => {
let sql = 'INSERT INTO GIFTICON VALUES (null, ?, ?, ?, \'미사용\', now())';
let barcode_img = '/barcode_img/' + req.file.filename;
let name = req.body.name;
let exp_date = req.body.exp_date;
let used = req.body.used;
let deletedDate = req.body.deletedDate;
let params = [barcode_img, name, exp_date];
connection.query(sql, params,
(err, rows, fields) => {
// console.log(err);
res.send(rows);
}
);
});
app.delete('/api/gifticons:id', (req, res) => {
let sql = 'UPDATE GIFTICON SET used = now() WHERE id = ?';
let params = [req.params.id];
connection.query(sql, params,
(err, rows, fields) => {
res.send(rows);
}
)
});
app.listen(port, () => console.log(`Listening on port ${port}`));