-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
67 lines (55 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
63
64
65
66
67
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 4800;
const fs = require('fs');
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();
const multer = require('multer');
const upload = multer({dest: './upload'})
app.get('/api/movies',(req, res) =>{
connection.query(
"SELECT * FROM MOVIE WHERE isDeleted =0",
(err,rows,fields) => {
res.send(rows);
}
);
});
app.use('/image',express.static('./upload'));
app.post('/api/movies', upload.single('image'),(req, res) => {
let sql = 'INSERT INTO MOVIE VALUES(null, ?, ?, ?, ?, ?, now(), 0,"","")';
let image = '/image/' + req.file.filename;
let name = req.body.name;
let day = req.body.day;
let genre = req.body.genre;
let age = req.body.age;
let keyWord = req.body.keyWord;
let keyWord2 = req.body.keyWord2;
let params = [image, name, day, genre, age,keyWord,keyWord2]
connection.query(sql,params,
(err, rows, fields) => {
res.send(rows)
}
);
});
app.delete('/api/movies/:id', (req,res)=>{
let sql = 'UPDATE MOVIE SET isDeleted =1 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}`));