feat: 新增 nodejs 实验 8
This commit is contained in:
41
Server/tests/test8/routes/index.js
Normal file
41
Server/tests/test8/routes/index.js
Normal file
@@ -0,0 +1,41 @@
|
||||
const db = require("../db");
|
||||
|
||||
const handlerLogin = async (ctx) => {
|
||||
const { username, password } = ctx.request.body;
|
||||
console.log(username + password);
|
||||
|
||||
const [rows] = await db.query("select * from users where username = ? ", [username]);
|
||||
if (rows.length > 0) {
|
||||
const user = rows[0];
|
||||
console.log(user.password, password);
|
||||
|
||||
if (user.password === password) {
|
||||
ctx.body = "登录成功";
|
||||
} else {
|
||||
ctx.body = "密码错误";
|
||||
}
|
||||
} else {
|
||||
ctx.body = "用户名不存在";
|
||||
}
|
||||
}
|
||||
|
||||
const handlerRegister = async (ctx) => {
|
||||
const { username, password, email, gender, hobbies, city, description } = ctx.request.body;
|
||||
try {
|
||||
await db.query("insert into users(username, password, email, gender, hobbies, city, description) values(?, ?, ?, ?, ?, ?, ?)", [username, password, email, gender, JSON.stringify(hobbies), city, description]);
|
||||
ctx.status = 201;
|
||||
ctx.body = {
|
||||
message: "注册成功"
|
||||
};
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
ctx.status = 500;
|
||||
ctx.body = {
|
||||
error: "服务器内部错误, 请稍后重试"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
handlerLogin, handlerRegister
|
||||
}
|
||||
Reference in New Issue
Block a user