Express and Database (MySQL) interaction (ii)

Source: Internet
Author: User
Tags sendfile

First, install Express in front of all said

1.express.

CNPM | | NPM Install Express--save

2. Middleware

A.CNPM | | NPM Install Body-parser--save When a post is requested, this middleware is needed to process the parameters

B.CNPM | | NPM install Cookie-parser processing cookie Middleware

C. cnpm | | NPM Install Multer handles file uploads in form forms

3. Installing MySQL's database integration

CNPM | | NPM install MySQL

This operation is dependent on the MySQL boot data in the Phpstudy is also used with the Phpstudy self-contained server. Need to download a phpstudy for yourself

Second, link database

varExpress = require (' Express ');varApp =Express ();varMySQL = require (' MySQL ');varCon =mysql.createconnection ({host:' localhost ',//Database ServerUser: ' Root ',//User namePassword: ' 511025 ',//PasswordDatabase: ' UserInformation ',//Database}); Con.connect (); App.get (‘/‘,function(req,res) {res.sendfile (__dirname+ '/' + ' form.html ');}); App.get ('/form.html ',function(req,res) {res.sendfile (__dirname+ '/' + ' form.html ');}); App.get ('/keepuserinformation ',function(req,res) {varsql = ' INSERT into usertable (user_id,user_name,user_sex,user_phone,user_origin, ' + ' User_email) VALUES (,?,?,?,?,?) ‘; varReqdata =[Req.query.user_name,req.query.user_sex, Req.query.user_phone,req.query.user_origin, Req.query.user_ema    IL]; Con.query (Sql,reqdata,function(error,result) {if(Error) {Res.json ({code:300,msg: ' Insert failed '}); }Else{Res.json ({code:200,msg: ' Insert Success ', data:[]}); });});varServer = App.listen (' 3000 ',function() {Console.log (' Server start 127.0.0.1 ');});

2.form.html

<!    DOCTYPE html>User name:<input type= "text" name= "user_name" value= "MySQL" ><br>User gender:<input type= "text" name= "User_sex" value= "female" ><br>User address:<input type= "text" name= "User_origin" value= "Phpstudy" ><br>User phone:<input type= "text" name= "User_phone" value= "><br>"User email:<input type= "text" name= "User_email" value= "[email protected]" ><br></form> <div class= "Keep" > Save </div></body><script type= "Text/javascript" >    $(". Keep"). On ("click",function() {$.ajax ({URL:"/keepuserinformation", data:$ (' Form '). Serialize (), type:' GET ', Success:function(res) {Console.log (res);    }        }); });</script>

3.

4.

5.

Three, express write interface, and the database to give a simple example.

All complex business logic is made up of simple sample. It's easy to figure out every single point.

Node's introductory chapter is almost over. Interested can look at the Ejs,jade template engine.

Come on!

var a = {N:2};

var B = A;

a.x = a = {N:1};

B.x?

A.x?

Express and Database (MySQL) interaction (ii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.