First, login page
This is nothing to say, just put two pictures
Second, server-side
Use Express (document) to build a server, data pants with MySQL (basic statement), create a new users, and then create a new users table, we use this table.
The main server is to write a simple interface to handle the page sent over the request.
//Introducing DependenciesvarExpress = require (' Express ');varurl = require (' URL '));varBodyparser = require (' Body-parser '));varApp =Express ();
//Core Logic module varsql = require ('./login.js ')); App.use (bodyparser.urlencoded ({extended:true}));//Setting the response headerApp.all (' * ',function(req, res, next) {Res.header ("Access-control-allow-origin", "*"); Res.header ("Access-control-allow-methods", "Put,post,get,delete,options"); Res.header (' Access-control-allow-headers ', ' X-requested-with,content-type,access-control-allow-origin '); Res.header ("X-powered-by", ' 3.2.1 ') Res.header ("Content-type", "Application/json;charset=utf-8"); Next ();});//Exposed InterfaceApp.post ('/user ',function(req, res) {varquery = Url.parse (Req.url,true). Query; Switch(query.ctr) { Case' Add ': Sql.catchres ({type:' INSERT ', data:req.body},function(msg) {res.send (msg); }); Break; Case' Update ': Sql.catchres ({type:' UPDATE ', data:req.body},function(msg) {res.send (msg); }); Break; Case' Delete ': Sql.catchres ({type:' DELETE ', data:req.body},function(msg) {res.send (msg); }); Break; Case' Login ': Sql.catchres ({type:' SELECT ', data:req.body},function(msg) {res.send (msg); }); Break; default: Res.send (' Undefined contrl! '); } });//Listening PortApp.listen (' 8080 ',function() {Console.log (' Listen at 8080 ')})
Third, the operation
Node App.js
Our server starts up, listens on port 8080, and we initiate a request to port 8080:
original code in my GitHub warehouse, welcome to download:https://github.com/lastnigtic/node-login
Simple front-end separation User Login system based on native Js+node.js+mysql