const express = require('express')
const bodyparse = require('body-parser');
const app = express()
const port = 3000
app.use(express.static('public'))
app.use(bodyparse.urlencoded({
extended: false }))
let db = [
{
username: 'admin', password:"123456"},
{
username: '张三丰', password:"qq8899"},
];
app.get('/', (req, res) => res.send('Hello World!'));
app.post('/user/register', (req, res) => {
console.log(req.body);
let {
regname, regpass} = req.body;
let result = db.some((item) => {
return item.username == regname;
});
if (result) {
res.send({
code:1, msg: "用户名已经存在"})
} else {
db.push( {
username: regname, password: regpass} )
res.send({
code:0, msg: "注册成功"})
}
})
app.post('/user/login', (req,res) => {
let {
logname, logpass} = req.body;
let result = db.find((item) => {
return item.username == logname && item.password == logpass;
});
if (result) {
res.json({
code:0, msg: "登录成功", username: result.username,})
} else {
res.json({
code:1, msg: "用户名或密码错误"})
}
})
app.listen(port, () => console.log(`服务器已经成功启动啦~`))
<