When you connect to a database, you need code to manipulate it, pass in the MySQL statement, and the parameters, and then the callback.
New
Added App.post ('/process_post ', Urlencodedparser, function (req, res) {//post processing method var response = {"n Ames ": req.body.names,//Get page Submitted data" passwords ": req.body.passwords}; Link to the repository var mysql = require ("MySQL"); var connection = Mysql.createconnection ({//config parameter, then add the table inside your database host: ' Database Address ', User: ' username ', password: ' Password ', database: ' DB name '}) Connection.connect (); Connect//Insert the var addsql = "INSERT into formtable (Name,password) VALUES (?,?)"; MySQL statement var addparmas = [Response.names, response.passwords];//Pass parameters into Connection.query (Addsql, Addparmas, func tion (err, res) {if (err) {Console.log ("[Insert error]-", err.message); Return }else{Show (RES)//Callback}}) function Show (arr) {Result.data=arr; Res.json (result);//Return to front end res.end (); } })
The urlencodedparser above is a code interpretation tool
Delete
Delete Data app.post ('/del ', Urlencodedparser, function (req, res) { //post processing method var response = { "IDs": Req.body.id //Get the data submitted by the page }; Link to the repository var mysql = require ("MySQL"); var connection = mysql.createconnection ({ //config parameter, then add the table inside your database host: ' Database Address ', User : ' username ', Password: ' Password ', database: ' DB name ' }) connection.connect (); Connect var userdelsql = ' DELETE from formtable WHERE id =? '; var addparmas = [Response.ids]; Connection.query (Userdelsql,addparmas,function (err, res) { if (err) { console.log (' [Select error]-', Err.message); return; } else{ Show (res) } }) function Show (arr) { Result.data=arr; Res.json (result); Res.end (); } })
Inquire
Initialize app.post ('/init ', Urlencodedparser, function (req, res) { //post processing method var response = { "names": Req.body.names //Get the data submitted by the page }; Link to the repository var mysql = require ("MySQL"); var connection = mysql.createconnection ({ //config parameter, then add the table inside your database host: ' Database Address ', User : ' username ', Password: ' Password ', database: ' DB name ' }) connection.connect (); Connection var selectsql = "Select * from formtable"; Connection.query (Selectsql, function (err, res) { if (err) { console.log (' [Select error]-', err.message); return; } else{ Show (res) } }) function Show (arr) { Result.data=arr; Res.json (result); Res.end (); } })
Modify
Modify Data app.post ('/modify ', Urlencodedparser, function (req, res) {//post processing method var response = {"IDs": r Eq.body.ids,//Get page Submitted data "names": Req.body.names, "Passwords": req.body.passwords}; Link to the repository var mysql = require ("MySQL"); var connection = Mysql.createconnection ({//config parameter, then add the table inside your database host: ' Database Address ', User: ' username ', password: ' Password ', database: ' DB name '}) Connection.connect (); Connect var usermodsql = ' UPDATE formtable SET name =?, Password =? WHERE id =? '; var addparmas = [Response.names,response.passwords,response.ids]; Console.log (Addparmas); Connection.query (Usermodsql,addparmas,function (err, res) {if (err) {console.log (' [Select error]-' , err.message); Return }else{Show (RES)}}) function Show (arr) {Result.data=arr; Res.json (result); Res.end (); } })
Above the "/modify" '/del ' '/init '/process_post ' This four is four interface, read the above four you will feel connected to the database add and delete to find nothing, of course, about the method inside you can package it yourself, increase his reusability
Full code
Table Price From_submit (); For ease of management I call var result = {"status": "Success",}//table value function From_su Bmit () {var express = require (' Express '); Call template var app = Express (); Does not pollute originally, uses the variable to represent the var Bodyparser = require (' Body-parser '); Call template var mysql = require (' mysql '); App.use (express.static (' public ')); Set today's file directory//app.get ('/form_index.html ', function (req,res) {//Res.sendfile (__dirname+ "/" +form_index.html "); Provide static files//})//Set cross-domain Access App.all (' * ', function (req, res, next) {Res.header ("Access-control-allow-origin", "*"); Res.header ("Access-control-allow-headers", "X-requested-with"); Res.header ("Access-control-allow-methods", "put,post,get,delete,options"); Res.header ("x-powered-by", ' 3.2.1 '); Res.header ("Content-type", "application/json;charset=utf-8"); Next (); }); Create code parse var urlencodedparser = bodyparser.urlencoded ({extended:false})//New App.post ('/process_post ', Urlencodedparser, Function (req, res) {//post processing method//Output JSON format var response = {"Names": Req . Body.names,//Get page Submitted data "passwords": req.body.passwords}; Link to the repository var mysql = require ("MySQL"); var connection = Mysql.createconnection ({//config parameter, then add table inside your database host: ' localhost ', User: ' Root ', pass Word: ' Root ', database: ' Test '}) Connection.connect (); Connect//Insert the var addsql = "INSERT into formtable (Name,password) VALUES (?,?)"; Storing the database language, here is the addition of var addparmas = [Response.names, response.passwords]; Connection.query (Addsql, Addparmas, function (err, res) {if (err) {Console.log ("[Insert error]-", er R.message); Return }else{Show (RES)}}) function Show (arr) {Result.data=arr; Res.json (result); Res.end (); }})//Initialize App.post ('/init ', Urlencodedparser, function (req, res) {//post processing method//Output JSON format var response = {"Names": req.body.names//Get the number of page commits The data}; Link to the repository var mysql = require ("MySQL"); var connection = Mysql.createconnection ({//config parameter, then add table inside your database host: ' localhost ', User: ' Root ', pass Word: ' Root ', database: ' Test '}) Connection.connect (); Connection var selectsql = "Select * from formtable"; Connection.query (Selectsql, function (err, res) {if (err) {console.log (' [Select error]-', err.mess Age); Return }else{Show (RES)}}) function Show (arr) {Result.data=arr; Res.json (result); Res.end (); }})//delete data app.post ('/del ', Urlencodedparser, function (req, res) {//post processing method//Output JSON format var response = {"IDs": req.body.id//Get data submitted by the page}; Link to the repository var mysql = require ("MySQL"); VarConnection = Mysql.createconnection ({//config parameter, then add table inside your database host: ' localhost ', User: ' Root ', password : ' Root ', database: ' Test '}) Connection.connect (); Connect var userdelsql = ' DELETE from formtable WHERE id =? '; var addparmas = [Response.ids]; Connection.query (Userdelsql,addparmas,function (err, res) {if (err) {console.log (' [Select error]-' , err.message); Return }else{Show (RES)}}) function Show (arr) {Result.data=arr; Res.json (result); Res.end (); }})//Modify Data app.post ('/modify ', Urlencodedparser, function (req, res) {//post processing method//Output JSON format var response = {"IDs": req.body.ids,//Get page Submitted data "names": Req.body.names, "passwords": r Eq.body.passwords}; Link to the repository var mysql = require ("MySQL"); var connection = Mysql.createconnection ({//config parameter, thenAdd the table inside your database host: ' localhost ', User: ' Root ', password: ' Root ', database: ' Test '}) Conne Ction.connect (); Connect var usermodsql = ' UPDATE formtable SET name =?, Password =? WHERE id =? '; var addparmas = [Response.names,response.passwords,response.ids]; Console.log (Addparmas); Connection.query (Usermodsql,addparmas,function (err, res) {if (err) {console.log (' [Select error]-' , err.message); Return }else{Show (RES)}}) function Show (arr) {Result.data=arr; Res.json (result); Res.end (); }}) var server = App.listen (, function () {//monitor var host = Server.address (). address var port = Serve R.address (). Port Console.log ("application instance, Access address http://%s:%s", host, Port)})}
Writing this is mainly convenient for me to study, if there are good suggestions can be exchanged or teach me a bit ~
Note: The formtable in the above code is one of the tables in my local database, and if you indicate that this is not the one that was passed in the MySQL statement after the link database was modified
Nodejs Connection database Additions and deletions