-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (52 loc) · 1.71 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
const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');
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 conn = mysql.createConnection({
host : conf.host,
user : conf.user,
password : conf.password,
port : conf.port,
database : conf.database
});
conn.connect();
const multer = require('multer');
const upload = multer({ dest : './upload' });
app.get('/api/hello', (req, res) => {
res.send({ message : 'Hello Express '});
});
app.get('/api/customers', (req, res) => {
conn.query(
"SELECT * FROM CUSTOMER WHERE isDeleted = 0",
(err, rows, fields) => {
res.send(rows);
}
);
});
app.use('/image', express.static('./upload')); //upload 폴더 공유
app.post('/api/customers', upload.single('image'), (req, res) => {
let sql = 'INSERT INTO CUSTOMER VALUE ( null, ?, ?, ?, ?, ?, now(), 0)';
let image = '/image/' + req.file.filename;
let name = req.body.name;
let birthday = req.body.birthday;
let gender = req.body.gender;
let job = req.body.job;
let params = [image, name, birthday, gender, job];
conn.query(sql, params, (err, rows, fields) => {
res.send(rows);
});
});
app.delete('/api/customers/:id', (req, res) => {
let sql = 'UPDATE CUSTOMER SET isDeleted = 1 WHERE id = ?';
let params = [req.params.id];
conn.query(sql, params, (err, rows, fields) => {
res.send(rows);
});
});
app.listen(port, () => console.log(`Listening on port ${port}`));