-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
83 lines (67 loc) · 2.07 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
const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');
// 파일 처리를 위한 라이브러리
const multer = require('multer');
const app=express();
const port = process.env.port || 5000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended : true}));
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();
// app.get('/api/hello', (req, res) => {
// res.send({message : 'Hello Server Excpress'});
// });
/* 전체 조회 */
app.get('/api/customers', (req,res) => {
connection.query(
"SELECT * FROM customer WHERE isDeleted = 0",
(err, rows, fields) => {
res.send(rows);
}
)
});
/* 고객 추가 */
const upload = multer({dest : './upload'});
app.use('/image',express.static('./upload'));
app.post('/api/customers', upload.single('image'), (req, res) => {
let sql = 'INSERT INTO customer VALUES ( null, ?, ?, ?, ?, ?, now(), 0)';
let image = '/image/' + req.file.filename;
let name = req.body.userName;
let birthday = req.body.birthday;
let gender = req.body.gender;
let job = req.body.job;
// console.log(image);
// console.log(name);
// console.log(birthday);
// console.log(gender);
// console.log(job);
let params = [image, name, birthday, gender, job];
connection.query(sql, params,
(err, rows, fields) => {
res.send(rows);
//console.log(err);
}
);
});
/* 고객 삭제 */
app.delete('/api/customers/:id', (req,res) => {
let sql = 'UPDATE customer SET isDeleted = 1 WHERE id = ?';
let params = [req.params.id];
connection.query(sql, params,
(err, rows, fields) =>{
res.send(rows);
console.log(err);
}
);
});
app.listen(port, () => console.log(`Listening on port ${port}`));