[Node.js-8] A simple login case

1. The directory structure of the entire project is as follows:
write picture description here

2. The main adjustment here is that it is still a unified entry server.js, but in this entry, requirethe routes of different modules are separated. This ensures the simplicity of the main entry file.

const express = require('express');
const expressStatic = require('express-static');
const bodyParser = require('body-parser');
const multer = require('multer');
const multerObj = multer({dest:'./static/upload'});
const cookieParser = require('cookie-parser');
const cookieSession = require('cookie-session');
const consolidate = require('consolidate');
const mysql = require('mysql');

var server = express();
server.use(bodyParser());
server.use(cookieParser());
var keys = [];
for(var i=0;i<10000;i++){
    keys[i]="key_"+Math.random();
}
server.use(cookieSession({
    name:'session_id',
    keys:keys,
    maxAge:20*60*1000
}));

// 模板
server.engine('html',consolidate.ejs);
server.set('view engine','html');
server.set('views','template');

// 路由
server.use('/admin',require('./route/admin.js')());
server.use('/web',require('./route/web.js')());

server.use(expressStatic('./static'));
server.listen(1337);

3. Here are our two sub-modules:

const express = require('express');
const md5 = require('md5');
const mysql = require('mysql');

module.exports=function(){
    var router = express.Router();

    router.get('/login',function(req,res){
        res.render('./web/login.html',{login_tip:''});
    });

    router.get('/index',function(req,res){
        res.send("i am web index page").end();
    });

    var db = mysql.createPool({
        host:'localhost',
        user:'root',
        password:'root',
        database:'nodejs'
    });

    router.post('/loginauth',function(req,res){
        var newPwd = md5(req.body.pwd);
        console.log(req.body.username,newPwd);
        // sql语句是用反引号括起来的
        db.query(`select * from n_user where USER_NAME='${req.body.username}' and USER_PWD='${newPwd}';`,(err,data)=>{
            if (err) {
                // 报错
                res.send(err).end();
            }else{
                console.log(data.length);
                // 判断是否有查询结果
                if (data.length==0) {
                    res.render('./web/login.html',{login_tip:'用户名或密码错误'});
                }else{
                    res.redirect('/web/index');
                }
            }
        });
    });

    return router;
}

4. It should be noted here that we use the res.redirectmethod to redirect, and the result of querying the database here can be used to data.lengthjudge whether there is a result. We still use md5the module here. This official one seems to be adopted crypto, but it is md5more concise to use it directly here.

5. You can also use the above cookie-sessionto judge whether you have logged in. It will not be written here. The source code is as follows: Node.js simple login case

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325826868&siteId=291194637