1. 程式人生 > >nodejs登入(ajax,session,無加密,跳轉,無分頁)

nodejs登入(ajax,session,無加密,跳轉,無分頁)

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div>
        使用者名稱:<input type="text" id="uname"><br/>
        密碼:<input type="password" id="upwd"><br/>
        <input type="submit" value="提交" id="btn_smt">
    </div>

    <script src="js/jquery.min.js"></script>
    <script>
        $("#btn_smt").click(function(){
            var uname=$("#uname").val();
            var upwd=$("#upwd").val();
            $.ajax({
                type:"post",
                url:"/login",
                data:{uname:uname,upwd:upwd},
                success:function(data){
                    if(data.code>0){
                        location.href="userlist.html";
                    }else{
                        alert(data.msg);
                    }
                },
                error:function(){
                    alert("網路故障,請檢查");
                }
            })
        });
    </script>
</body>
</html>
userlist.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        a{
            color: red;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <a id="logout">退出</a><br/>
    <table>
        <thead>
            <tr>
                <td>tid</td>
                <td>tname</td>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>

    <script src="js/jquery.min.js"></script>
    <script>
        $.ajax({
            type:"get",
            url:"/userlist",
            success:function(data){
                if(data.code==-1){
                    location.href="login.html";
                }
                var html="";
                for(var i=0;i<data.length;i++){
                    html+="<tr>";
                    html+="<td>"+data[i].uid+"</td>";
                    html+="<td>"+data[i].uname+"</td>";
                    html+="</tr>";
                }
                $("tbody").html(html);
            },
            error:function(){
                alert("網路故障,請檢查");
            }
        });

        $("#logout").click(function(){
            $.ajax({
                type:"get",
                url:"/logout",
                success:function(data){
                    if(data.code==1){
                        location.href="login.html";
                    }
                },
                error:function(){
                    alert("網路故障,請檢查");
                }
            });
        });
    </script>
</body>
</html>
app.js
/**
 * Created by web-01 on 2018/1/5.
 */
//1、載入對應模組
var http=require("http");
var express=require("express");
var session=require("express-session");
var cookieParser=require("cookie-parser");
var bodyparser=require("body-parser");
//2、建立連線池
var m=require("./init.js");
var pool=m.pool;
//3、建立伺服器 3000
var app=express();
var server=http.createServer(app);
server.listen(3000);
//4、載入靜態目錄 public
app.use(express.static('public'));
//5、配置 body-parser
app.use(bodyparser.urlencoded({extended:false})); //bodyparser全小寫
//6、配置session
app.use(cookieParser());//這裡要加()
app.use(session({
    secret:'spflinux',
    name:'adminapp',
    cookie:{maxAge:60*1000*30},
    resave: false,
    saveUninitialized: true
}));

//7、處理登入請求
app.post("/login",(req,res)=>{
    var uname=req.body.uname;
    var upwd=req.body.upwd;
    var sql="SELECT uid FROM t_user WHERE uname=? AND binary upwd=?"; //一定記得加binary區分大小寫
    pool.getConnection((err,conn)=>{
        if(err) throw err;
        conn.query(sql,[uname,upwd],(err,result)=>{
            if(err) throw err;
            if(result.length>0){
                req.session.sign=true;
                res.json({code:1,msg:"登入成功"});
            }else{
                res.json({code:-1,msg:"使用者名稱或密碼有誤"});
            }
            conn.release();
        })
    })
})

app.get("/",(req,res)=>{
        res.sendFile(__dirname+"/public/"+"login.html");
})

app.get('/userlist',(req,res)=>{
    console.log("req.session.sign="+req.session.sign);
    if(req.session.sign==true){
        var sql="SELECT uid,uname FROM t_user";
        pool.getConnection((err,conn)=>{
            if(err) throw err;
            conn.query(sql,(err,result)=>{
                if(err) throw err;
                res.json(result);
            });conn.release();
        })
   }else{
        res.json({code:-1,msg:"需登入"});
    }
})

app.get('/logout',(req,res)=>{
    req.session.sign=false;
    //console.log("退出");
    res.json({code:1,msg:"退出"});
})

init.js

/**
 * Created by web-01 on 2018/1/5.
 */
const mysql=require("mysql");
var pool=mysql.createPool({
    host:'127.0.0.1',
    user:"root",
    password:"",
    database:"test",
    port:3306,
    connectionLimit:25 //連線數量限制
});

module.exports.pool=pool;