server.js 2.34 KB
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}`));