# 二.学生成绩表增删改查

# 1.数据库

这里以 mysql 为例,启动 mysql,使用 node 编写查询语句

# 2.后端接口

/server.js

const http = require("http")
const url = require("url")
const querystring = require("querystring")
const { sql_add, sql_delete, sql_update, sql_query } = require("./mysql")
const port = "3000"
const server = http.createServer()
server.listen(port, function() {
  console.log(`已经连接${port}端口`)
})

let query = (res, req, url, result) => {
  if (req.url.includes("favicon.ico")) {
    return false
  }
  console.log("请求路径" + req.url)
  console.log("请求的方式:" + req.method)
  console.log("请求的路径:" + url)
  res.writeHead(200, {
    "Content-Type": "text/html;charset=utf-8",
  })
  console.log(result)
  res.write(JSON.stringify(result))
  res.end()
}

let common = {
  //查询全部
  queryAll: (res, req, url) => {
    sql_query((r) => {
      let result = { code: 200 }
      result.data = r
      query(res, req, url, result)
    })
  },
  //查询单个
  querySingle: (res, req, url) => {
    let data = ""
    req.on("data", (chunk) => {
      data += chunk
    })
    req.on("end", () => {
      sql_query((r) => {
        let result = { code: 200 }
        result.data = r
        query(res, req, url, result)
      })
    })
  },
  //添加单个
  addSingle: (res, req, url) => {
    let data = ""
    req.on("data", (chunk) => {
      data += chunk
    })
    // data = querystring.parse(result.data.toString());
    req.on("end", () => {
      data = querystring.parse(data.toString())
      console.log("请求数据:", data)
      sql_add(data.s_name, data.s_english, data.s_math, (r) => {
        sql_query((r) => {
          let result = { code: 200 }
          result.data = r
          query(res, req, url, result)
        })
      })
    })
  },
  //删除单个
  deleteSingle: (res, req, url) => {
    let data = ""
    req.on("data", (chunk) => {
      data += chunk
    })
    req.on("end", () => {
      data = querystring.parse(data.toString())
      console.log("请求数据:", data)
      sql_delete(data.id, (r) => {
        sql_query((r) => {
          let result = { code: 200 }
          result.data = r
          query(res, req, url, result)
        })
      })
    })
  },
  //更新
  update: (res, req, url) => {
    let data = ""
    req.on("data", (chunk) => {
      data += chunk
    })
    req.on("end", () => {
      data = querystring.parse(data.toString())
      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
          query(res, req, url, result)
        })
      })
    })
  },
}
server.on("request", function(req, res) {
  let arr = ["queryAll", "querySingle", "addSingle", "deleteSingle", "update"]
  for (let i = 0; i < arr.length; i++) {
    if (req.url.includes(arr[i])) {
      common[arr[i]](res, req, arr[i])
    }
  }
})
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

# 3.启动

修改package.json

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
1
2
3
4

运行

npm run start
1

# 4.使用接口测试工具测试