server.js
2.34 KB
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
const fs= require('fs');
const express=require('express');
const bodyparser = require("body-parser");
const app = express();
const port = process.env.process || 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();
const multer=require('multer');
const upload = multer({dest:'./upload'})
app.get('/api/students', (req, res)=>{
connection.query(
"SELECT * FROM dbproject.Students WHERE isDeleted=0",
(err,rows,fields)=>{
res.send(rows);
console.log(err);
}
);
});
app.use('/image', express.static('./upload'));
app.post('/api/students',upload.single('image'), (req, res) =>{
let sql = 'INSERT INTO dbproject.Students VALUES (?,?,?,?,?,?,?,?,?,now)';
let st_Code= req.body.st_Code;
//let st_Name = req.body.st_Name;
//let st_Id = req.body.st_Id;
let st_Major = req.body.st_Major;
let st_Midscore = req.body.st_Midscore;
let st_Finalscore = req.body.st_Finalscore;
let st_Assignscore = req.body.st_Assignscore;
let st_Attendscore = req.body.st_Attendscore;
let st_Score = req.body.st_Score;
console.log(st_Code);
//console.log(st_Name);
//console.log(st_Id);
console.log(st_Major);
console.log(st_Midscore);
console.log(st_Finalscore);
console.log(st_Assignscore);
console.log(st_Attendscore);
console.log(st_Score);
let params=[st_Code,
//st_Name,
//st_Id,
st_Major,
st_Midscore,
st_Finalscore,
st_Assignscore,
st_Attendscore,
st_Score];
connection.query(sql, params,
(err,rows,fields)=>{
res.send(rows);
console.log(err);
}
);
});
app.delete('/api/students/:st_Code',(req,res)=>{
let sql='UPDATE dbproject.Students SET isDeleted =1 WHERE st_Code = ?';
let params =[req.params.st_Code];
connection.query(sql, params,
(err,rows,fields)=>{
res.send(rows);
console.log(err);
}
);
});
app.listen(port, () => console.log(`Listening on port ${port}`));