Home  >  Q&A  >  body text

javascript - nodejs login system controls static page jump

I want to make a demonstration system quickly. Due to the lack of background, I want to build it myself using nodejs.
Although they are all static pages, the login function needs to be completed (no registration is required, the password can be assigned directly to the database), and the jump can only be made after logging in. Go to the homepage of the page. If you enter the homepage path directly without logging in, it will jump to the login page.
Because I am a novice and I don’t know how to do it. Please help me with the direction and how to complete it quickly

高洛峰高洛峰2687 days ago812

reply all(3)I'll reply

  • 漂亮男人

    漂亮男人2017-06-10 09:51:02

    You need koa and koa-router

    reply
    0
  • 代言

    代言2017-06-10 09:51:02

    You can use express session and then write a checkLogin method to determine whether !req.session.user is logged in. Then write a checkNoLogin method and judge req.session.user.

    reply
    0
  • 漂亮男人

    漂亮男人2017-06-10 09:51:02

    I won’t write the html for you, the basic form content
    JS ajax request:

    $("#userLogin").click(function(){
            $.ajax({
              url: "/login",
              type: "GET",
              data: {
                  username: $("#email-one").val(),    //    用户名和密码
                  password: $("#password-one").val()
              },
              success: function(data){
                  if(data.status==1){
                  window.location.href='index.html';    //    请求成功后到你的主页
                  }else{
                      alert(data.msg);
                  }
                      
              },
              error: function(){
                layer.msg('访问失败', hint);
              }
          });
        });

    If you don’t understand the code below, you can refer to /a/11...

    var express=require("express");
    var events = require('events');
    var app=express();
    var session=require("session");
    var path=require("path");
    var mysql=require("mysql");
    var dirname=__dirname;
    app.use(express.static(path.join(__dirname, 'project')));
    app.get("/login",function(req,res){
        //    连接数据库
        var connection=mysql.createConnection({
            host:"localhost",
            user:"root",
            password:"数据库密码",
            database:"node"
        });
        connection.connect();
        var sql="select * from user where username='"+req.query.username+"' and password='"+req.query.password+"'";
        connection.query(sql,function(err,result){
            if(err){
                res.end("登陆失败");
            }
            if(result.length==0){
                res.json({status:0,msg:“用户名或密码不正确”});
            }else{
                req.session.user=req.query.username;
                req.session.isLogin=true;
                res.json({status:1,msg:“登录成功”});
            }
        })
        connection.end();
    })
    app.listen(8081);

    Access to subsequent pages must have session information. You can make requests to the background on each page. Then you use node to detect the session content, and then ajax takes action. The general idea is this, I am also a novice, I hope I can help you.

    reply
    0
  • Cancelreply