Log database lookup and redis check code

`
 @ To generate verification mA 
const svgCaptcha = the require ( " SVG-captcha " ) 

// MySQL database lookup for the front-end input user name and password 
const MySQL = the require ( ' MySQL ' ); 

const Connection = MySQL .createConnection ({ 
  Host: ' localhost ' , 
  User: ' the root ' , 
  password: ' 12345678 ' , 
  database: ' SYS ' 
}); 

connection.connect (); 

// codes redis database for storing a randomly generated
// facilitate timely update 

const Client = redis.createClient (); 

// Redis If it does not redis, being given
 // Redis-Server start command Redis
 // new terminal-input CLI Redis 

client.on ( " error " , function ( ERR) { 
  the console.log ( " Trror " + ERR); 
}) 

// generates a token and easy replacement redis storage 
function the getID (length) {
   return Number The (Math.random () toString () substr (.. . 3 , length) Date.now + ()) toString (. 36 ); 
} 


  // codes 
 App. GET ( "/ CAP " , (REQ, RES) => {
    const CAP = svgCaptcha.create ();
    // CAP => {Data: ..., text: jsoa}
    // text: randomly generated four codes
    @ data: image transferred to the front end of the codes of 
   
   
   the let text = cap.text; 
   res.type ( " SVG " ); 
   
   // generates an ID redis stored and passed to the front end of 
   the let captchaId the getID = ( 10 ) 
   Client. SET (captchaId, text) 
   res.send ({ 
     CAP, 
     captchaId 
   }) 
   return 
 }) 

  // distal reference picture
  //  <span dangerouslySetInnerHTML={{ __html: captcha }} onClick={() => this.getcaptcha()}></span>
 
 
 
 //登陆      
app.post("/getlogin", async (req, res) => {
   let { username, password, verification, captchaId } = req.body;
        let redisCaptcha = await new Promise((resolve) => {
          client.get(captchaId, function (err, res) {
            return resolve(res)
          })
        })

        if (verification === redisCaptcha) {
        } else{ 
          Res.send ({ 
            code: 0 , 
            Message: " code is incorrect or outdated " 
          }) 
          return 
        } 

     // database lookup data 
      connection.query (the FROM Table `the SELECT * WHERE username = ' $ {username} ' && passwrd = ' password} {$ ' `, function (error, Results, Fields) {
           IF (error) the throw error;
           IF (Results [ 0 ]) { 
            res.send ({code: . 1 , Message: " login successful" }) 
          } The else { 
            res.send ({code: 0 , Message: " user name or password error " })
             return 
          } 
        }); 

      }) 
` ``

 

Guess you like

Origin www.cnblogs.com/p-123/p/11456357.html