You need to create a new four files first
A server JS
A txt that holds data
A login, a registration page HTML
1. Registration page
<! DOCTYPE html> for= "User" > Username </label><input type= "text" id= "user" > </div> <div> <label for= "Password" > Secret code </label><input type= "password" id= "password" > </div > <div> <button id= "register" > Registration </button> </div></body><script src= "http: Libs.baidu.com/jquery/2.0.0/jquery.min.js "></script><script> $(function () { $("#register"). Click (function() {$.ajax ({URL:"Http://localhost:3000/register", type:"POST", data:{username:$ ("#user"). Val (), password:$ ("#password"). Val ()}, Success:function(res) {alert (res); }, Error:function(Err) {Console.log (err); } }) }) });</script>View Code2. Login interface
1<! DOCTYPE html>234<meta charset= "UTF-8" >5<title>login</title>67<body>8<div>9<label for= "User" > Username </label><input type= "text" id= "user" >Ten</div> One<div> A<label for= "Password" > Secret code </label><input type= "password" id= "password" > -</div> -<div> the<button id= "Login" > Login </button> -<button id= "register" ><a href= "regist.html" > Registration </a></button> -</div> -</body> +<script src= "Http://libs.baidu.com/jquery/2.0.0/jquery.min.js" ></script> -<script> +$(function () { A$ ("#login"). Click (function () { at if($ ("#user"). Val (). length = = 0){ - returnAlert ("Please enter content!")); - } - if($ ("#password"). Val (). length = = 0){ - returnAlert ("Please enter your password!")); - } in - $.ajax ({ toURL: "Http://localhost:3000/login", +Type: "POST", - data:{ theusername:$ ("#user"). Val (), *password:$ ("#password"). Val () $ },Panax NotoginsengSuccessfunction(res) { -Alert ("Login successful!") the }, +Errorfunction(err) { A Console.log (err); the } + }) - $ }) $ }); -</script> -View Code3. Build Server
1 varHTTP = require ("http");2 varurl = require ("url");3 varQS = require ("querystring");4 varFS = require ("FS");5 6Http.createserver (function(req, res) {7 //set the request header8Res.setheader ("Access-control-allow-origin", "*");9 if(Req.method = = "POST"){Ten //receive the user name and password sent One varresult = ""; A //get the routing address sent by the front-end code - varPathName =Url.parse (req.url). Pathname; -Req.addlistener ("Data",function(chunk) { theResult + =Chunk; - }); - -Req.on ("End",function () { + varuser =qs.parse (result); - //determine if the user exists + if(user.username) { AFs.readfile ("Db.txt", "Utf-8",function(err,data) { at if(!err) { -Console.log ("Read file succeeded"); - if(!data) { - if(PathName = = "/login"){ -Res.end ("The user does not exist"); - return; in } - //based on the routing address sent by the front end to determine whether the login or registration page, if the registration page to if(PathName = = "/register"){ + //Create an array an object to save the account number and password - vararr = []; the varobj = {}; * //Save the user's account password $Obj.username =User.username;Panax NotoginsengObj.password =User.password; - Arr.push (obj); the //synchronously writes the Db.txt file, which must be synchronized +Fs.writefilesync ("Db.txt", Json.stringify (arr), "Utf-8"); ARes.end ("Registered successfully!")); the return; + } -}Else { $Console.log ("Data in File"); $ //turn the data into JSON objects so that we can use the - vararr =json.parse (data); - //iterate through an array of saved data to determine login registration the for(vari = 0;i < arr.length;i++){ - varobj =Arr[i];Wuyi if(Obj.username = =user.username) { the if(PathName = = "/login"){ - if(Obj.password = =User.password) { WuRes.end ("Login Successful!"); - return; About}Else { $Res.end ("Bad password!") "); - return; - } - } A if(PathName = = "/register"){ +Res.end ("The user already exists!")); the return; - } $ } the } the if(PathName = = "/login"){ theRes.end ("User name does not exist!")); the return; - } in if(PathName = = "/register"){ the //To create a new object to write data the varobj = {}; AboutObj.username =User.username; theObj.password =User.password; the Arr.push (obj); theFs.writefilesync ("Db.txt", Json.stringify (arr), "Utf-8"); +Res.end ("Registered successfully!")); - return; the }Bayi } the}Else { theConsole.log ("Failed to read file"); - } - }) the } the }); the}Else { theRes.end ("Get Request"); - } the}). Listen (3000,function(err) { the if(!err) { theConsole.log ("Server started successfully, listening port3000 ...");94 } the});
4. You can view the registration information in the Db.txt file.
node. JS implements a simple login registration page