# 五.学生管理系统
# 1.数据库
这里以 mysql 为例,启动 mysql,使用 node 编写查询语句
# 2.后端接口
package.json
{
"name": "eg",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"express": "^4.16.4",
"http-errors": "~1.6.2",
"jade": "~1.11.0",
"morgan": "~1.9.0",
"mysql": "^2.16.0"
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/routes/add.js
var express = require("express")
var router = express.Router()
const { sql_query, sql_add } = require("../public/mysql")
var querystring = require("querystring")
router.all("*", function (req, res, next) {
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS")
res.header("Access-Control-Allow-Origin", "http://localhost:8080")
res.header("Content-Type", "application/json;charset=utf-8")
next()
})
/* GET home page. */
router.post("/", function (req, res, next) {
let data = req.body
sql_add(data.s_name, data.s_english, data.s_math, (r) => {
sql_query((r) => {
let result = { code: 200 }
result.data = r
res.send(result)
})
})
})
module.exports = router
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/routes/delete.js
var express = require("express")
var router = express.Router()
const { sql_query, sql_delete } = require("../public/mysql")
var querystring = require("querystring")
router.all("*", function (req, res, next) {
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS")
res.header("Access-Control-Allow-Origin", "http://localhost:8080")
res.header("Content-Type", "application/json;charset=utf-8")
next()
})
/* GET users listing. */
router.delete("/", function (req, res, next) {
let data = ""
req.on("data", (chunk) => {
data += chunk
})
req.on("end", () => {
data = querystring.parse(data.toString())
sql_delete(data.id, (r) => {
sql_query((r) => {
let result = { code: 200 }
result.data = r
res.send(result)
})
})
})
})
module.exports = router
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
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
/routes/query.js
var express = require("express")
var router = express.Router()
const { sql_query } = require("../public/mysql")
router.all("*", function (req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:8080")
res.header("Content-Type", "application/json;charset=utf-8")
next()
})
router.get("/", function (req, res, next) {
sql_query((r) => {
let result = { code: 200 }
result.data = r
res.send(result)
})
})
module.exports = router
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/routes/updata.js
var express = require("express")
var router = express.Router()
const { sql_update, sql_query } = require("../public/mysql")
var querystring = require("querystring")
router.all("*", function (req, res, next) {
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS")
res.header("Access-Control-Allow-Origin", "http://localhost:8080")
res.header("Content-Type", "application/json;charset=utf-8")
next()
})
/* GET users listing. */
router.put("/", function (req, res, next) {
let data = req.body
console.log(data)
sql_update(data.id, data.s_name, data.s_english, data.s_math, (r) => {
sql_query((r) => {
let result = { code: 200 }
result.data = r
res.send(result)
})
})
})
module.exports = router
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
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
app.js
var createError = require("http-errors")
var express = require("express")
var router = express.Router()
//设置请求头
router.all("*", function (req, res, next) {
// res.header("Access-Content-Allow-Origin", "*");
// res.header("Access-Control-Allow-Headers", "X-Requested-Width");
// res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
// res.header("X-Powered-By", "3.2.1");
res.header("Access-Control-Allow-Origin", "http://localhost:8080")
res.header("Content-Type", "application/json;charset=utf-8")
next()
})
//路径解析
var path = require("path")
//这就是一个解析cookie的工具,通过req.cookies可以取到传过来的cookie,并把他们转成对象
var cookieParser = require("cookie-parser")
//在控制台中,显示req请求的信息
var logger = require("morgan")
//node中间件,用于处理JSON,Raw,text和url编码的数据
var bodyParser = require("body-parser")
//路由信息(接口地址),存放在routes的根目录
var addRouter = require("./routes/add")
var deleteRouter = require("./routes/delete")
var updataRouter = require("./routes/updata")
var queryRouter = require("./routes/query")
var app = express()
// view engine setup
app.set("views", path.join(__dirname, "views"))
app.set("view engine", "jade")
//载入中间件
app.use(logger("dev"))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, "public")))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
//配置路由(自定义路径,上面设置的接口地址)
app.use("/add", addRouter)
app.use("/delete", deleteRouter)
app.use("/updata", updataRouter)
app.use("/query", queryRouter)
// 错误处理
app.use(function (req, res, next) {
next(createError(404))
})
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message
res.locals.error = req.app.get("env") === "development" ? err : {}
// render the error page
res.status(err.status || 500)
res.render("error")
})
module.exports = app
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
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