Since the emergence of node-js, not only Java, PHP and other backend languages can operate databases and add, delete, modify and check content. JavaScript simple language also has this skill. In node, JS has strong operability and code reading, making obscure and difficult-to-understand code clearer. Below I will briefly introduce how JavaScript operates databases and implements login and registration functions.
First of all, the page part is omitted directly. We have installed the database by default, so we start with login.
exports.login =function(req,res){ var username=req.body.uname; var pwd=req.body.psd; var sql="select * from student where username=? and password=?"; var con=dbcon.getCon(); con.query(sql,[username,pwd], function (err,result) { if(!err){ if(result.length==0){ res.json(0); }else{ res.json(1) } }else{ console.log(err) } con.destroy() })};First, click the login button, and we jump to the login page. By obtaining the input value entered by the user, obtaining the input value of username and pwd through the subscript method, and by connecting to the database, we judge the status value returned by the callback function. If successful, the user will be prompted to register successfully, and then the page will be redirected. If an exception occurs, give a prompt and jump to the error page accordingly. Here we can use the try{}catch(){} method to intercept and print out the specific error information.
Immediately afterwards, let’s talk about the registration operation. As the name suggests, registration means inserting user information that does not exist in the database into the database through the insert statement of the database. The principle is somewhat similar to login. First, we need to enter the information that needs to be registered, then obtain this information, and then send an asynchronous request to verify whether the database has this information. If not, create these information directly. Let's use code to illustrate these principles.
exports.regists=function(req,res,name,password){ var sql="insert into stuinfo(name,password) values(?,?)" conn.query(sql,[name,password],function(err,result){ if(result.affectedRows==1){ req.session.name=name; res.sendfile('./public/view.html') } })}exports.login=login;First, we will register this method for encapsulation, and then export it through node exports to facilitate call. In this method, insert username and password through placeholder method, and then insert the obtained parameter values through the database. Through the judgment of the callback function status code, we confirm that the insertion is successful, and after success, the session we registered successfully will be displayed on the page as a identifier.
The above is the simple login and registration code for js. The knowledge points involved are not only mentioned above, but also include basic knowledge of the database, understanding basic SQL query statements, and being able to successfully connect to the database through js. We always believe that the skyscrapers have risen from the ground. As long as we go, we can take one step at a time, one day we can become experts in the eyes of ourselves and others.
The above simple example of JS operation database registration and login is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.