-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
67 lines (62 loc) · 1.8 KB
/
Copy pathserver.js
File metadata and controls
67 lines (62 loc) · 1.8 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
var express = require("express");
var app = express();
var http = require("http").Server(app);
var mysql = require("mysql");
var connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "123456",
database: "test"
});
// 路由为/默认src静态文件夹
app.use("/", express.static(__dirname + "/src"));
connection.connect();
app.post("/register", function(req, res) {
const data = req.query;
const selectSql = `select * from user where name = '${data.name}'`;
const insertSql = `insert into user (name, password) values (?,?)`;
// 连接 SQL 并实施语句
connection.query(selectSql, (err, result) => {
if (err) {
return;
} else if (result.length === 0) {
connection.query(
insertSql,
[`${data.name}`, `${data.password}`],
(err, re) => {
if (re) {
res.send({ msg: "注册成功", code: 200 });
}
}
);
} else if (result.length !== 0) {
res.send({ msg: "用户名已存在", code: 201 });
}
});
// connection.end();
});
app.post("/login", function(req, res) {
const data = req.query;
const selectSql = `select * from user where name = '${data.name}'`;
connection.query(selectSql, (err, result) => {
if (err) {
return;
} else if (result.length === 0) {
res.send({ msg: "该用户不存在", code: 201 });
} else if (result.length !== 0) {
result = JSON.parse(JSON.stringify(result));
if (result[0].password == data.password) {
res.send({ msg: "登录成功", code: 200 });
} else {
res.send({ msg: "密码错误", code: 202 });
}
}
});
});
// app.all("/", function(req, res) {
// res.send("success", res.data);
// });
http.listen(3000, function() {
// 监听3000端口
console.log("listening on *:3000");
});