Express框架介绍

基于Express框架,运用get请求模拟简易注册登录案例。

const express = require('express');
let app = express();
app.use(express.urlencoded({expended: false}));

let users = {'admin': 12345, 'Tom': 123};

app.use('/', (req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    next();
});

app.get('/reg', (req, res) => {
    let {username, password} = req.query;
    // console.log(users);
    if(users[username]){
        res.send({flag: false, msg: '该账号已经存在'});
    }else{
        users[username] = password;
        res.send({flag: true, msg: '注册成功'});
    }
});

app.get('/login', (req, res) => {
    let {username, password} = req.query;
    // console.log(req.query)
    // console.log(users);
    if(users[username] == null){
        res.send({flag: false, msg: '此账号不存在'});
    }else if(users[username] != password){
        res.send({flag: false, msg: '密码错误'});
    }else{
        res.send({flag: true, msg: '登录成功'});
    }
});

app.listen(3000, ()=>{
    console.log('启动服务...');
});

 

基于Express框架,运用post请求模拟简易注册登录案例。 

const express = require('express');
let app = express();
app.use(express.urlencoded({expended: false}));

let users = {'admin': 12345, 'Tom': 123};

app.use('/', (req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    next();
});

app.post('/reg', (req, res) => {
    let {username, password} = req.body;
    if(users[username]){
        res.send({flag: false, msg: '该账号已经存在'});
    }else{
        users[username] = password;
        res.send({flag: true, msg: '注册成功'});
    }
});

app.post('/login', (req, res) => {
    let {username, password} = req.body;
    if(users[username] == null){
        res.send({flag: false, msg: '此账号不存在'});
    }else if(users[username] != password){
        res.send({flag: false, msg: '密码错误'});
    }else{
        res.send({flag: true, msg: '登录成功'});
    }
});

app.listen(3000, ()=>{
    console.log('启动服务...');
});

 

你可能感兴趣的:(nodejs,express,前端,javascript)