var express = require(`express`);
var mysql = require(`mysql`);
var app = express();
var bodyParser = require(`body-parser`);
//連結資料庫
var connection = mysql.createConnection({
host: `localhost`,
user: `root`,
password: `123456`,
database: `school`
});
connection.connect();
// 建立 application/x-www-form-urlencoded 編碼解析(post方法)
var urlencodedParser = bodyParser.urlencoded({ extended: false })
//設定跨域訪問
app.all(`*`, function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
//登入
app.post(`/login`,urlencodedParser, function (req, res) {
var username = req.body.username;
var password = req.body.password;
var sql = `select * from login where username = `${username}` and password = `${password}``;
connection.query(sql, function (err, result) {
console.log(result)
if (err || result.length == 0) {
res.status(200),
res.json("登陸失敗")
} else {
res.status(200),
res.json("登陸成功")
}
});
})
//查詢
app.get(`/query`, function (req, res) {
var sql = `select * from student`;
connection.query(sql, function (err, result) {
if (err) {
console.log(`err:`, err.message);
}
console.log(result);
res.status(200),
res.json(result)
});
});
//修改
app.get(`/change`, function (req, res) {
var SNO = req.query.SNO;
var SNAME = req.query.SNAME;
var SDEPT = req.query.SDEPT;
var sql = `update student set SNAME = `${SNAME}`,SDEPT = `${SDEPT}` where SNO = `${SNO}``;
connection.query(sql, function (err, result) {
if (err) {
console.log(`err:`, err.message);
}
console.log(result);
res.status(200),
res.json("修改成功")
});
})
//新增
app.get(`/add`, function (req, res) {
console.log(req.query)
var SNO = req.query.SNO;
var SNAME = req.query.SNAME;
var SDEPT = req.query.SDEPT;
var sql = `insert into student values (`${SNO}`,`${SNAME}`,`${SDEPT}`)`;
connection.query(sql, function (err, result) {
if (err) {
console.log(`err:`, err.message);
}
console.log(result);
res.status(200),
res.json("新增成功")
});
})
//刪除
app.get(`/delete`, function (req, res) {
console.log(req.query)
var SNO = req.query.SNO;
var sql = `delete from student where SNO=`${SNO}``;
connection.query(sql, function (err, result) {
if (err) {
console.log(`err:`, err.message);
}
console.log(result);
res.status(200),
res.json("刪除成功")
});
})
// connection.end();
//配置服務埠
var server = app.listen(8080, function () {
var host = server.address().address;
var port = server.address().port;
console.log(`http://`, host, port);
})
github:https://github.com/Rossy11/no…