Tag:Increasing and deletions of nodejs connection database
Category:node.js
Article From:https://www.cnblogs.com/ldlx-mars/p/9060682.html

When you connect to a database, you need to use code to import MySQL statements and parameters, and then callback.

Newly added

// Newly addedApp.post ('/process_post', urlencodedParser, function (req, RES) {//post processing methodVar reSponse = {{"Names": req.body.names, / / get the data submitted by the page."Passwords": req.body.passwoRDS};/ / linked databaseVar MySQL = require ("MySQL");Var connection = mysql.cReateConnection ({/ / configuration parameter, then add the table in your database.Host: 'database address',User: 'username',Password: 'cipher',Database: 'database name'})Connection.connect (); / / connection/ / insert dataVar addSql = "insert into formtable (name, password) values (??)"; //mysql statementVAR addParmas = [response.names, response.passwords]; / / transfer parameter entryConnection.query (addSql, addPar)MAS, function (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 the front endRes.end ();}})

  The above urlencodedParser is a coding interpretative tool.

delete

 //Delete dataApp.post ('/del', urlencodedParser, function (req, RES) {//post processing methodVar response= {"IDS": req.body.id / / get the data submitted by the page};/ / linked databaseVar MySQL = REquire ("MySQL");Var connection = mysql.createConnection ({/ / configuration parameter), then add the table in your database.HOst: 'database address',User: 'username',Password: 'cipher',Database: 'database name'})Connection.connect (); / / connectionVar 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 ();}})

  query

// InitializationApp.post ('/init', urlencodedParser, function (req, RES) {//post processing methodVar response= {"Names": req.body.names / / get the data submitted by the page.};/ / linked databaseVar mysqL = require ("MySQL");Var connection = mysql.createConnection ({/ / configuration parameter), then add the table in your database.Host: 'database address',User: 'username',Password: 'cipher',Database: 'database name'})Connection.connect (); / / connectionVar 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

//Modifying the dataApp.post ('/modify', urlencodedParser, function (req, RES) {//post processing methodVar respoNSE = {{"IDS": req.body.ids, / / get the data submitted by the page."Names": req.body.names,"Passwords": req.body.passwords};/ / linked databaseVar MySQL = require ("MySQL"));Var connection = mysql.createConnection ({/ / configuration parameter), then add the table in your database.Host: 'database address',User: 'username',Password: 'cipher',Database: 'database name'})ConNection.connect (); / / connectionVar userModSql ='UPDATE formtable SET name =?, password =? WHEREId =? ';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 ();}})

  The above “/modify”, “‘/del'”‘/init”/process_post’, the four is four interfaces, after reading the top four, you will find that the connection database is not changed and changed, of course, the inside of the method you can do the packaging one by yourself.Next, increase his reusability

Complete code

//Form price raisingFrom_submit (); / / in order to facilitate management, I use function to wrap up and call.Var result = {{"Status": "200","MEssage ":" success ",}/ / form price increaseFunction from_submit () {Var express = require ('expreSs'); / / call templateVar app = Express (); / / do not pollute the original, use variables to represent.Var bodyParser = require ('body-parser'); / / call templateVar MySQL = require ('mysql');App.use (express.static ('public')); / / set up today's documentsrecord/ / app.get ('/form_index.html', function (req, RES) {/ / res.sendFile (__dirname+ "/"+form_index.html); / / provides a static file/ /})/ / set cross domain accessApp.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; charse")T=utf-8 ");Next ();});/ / create coding parsingVar urlencodedParser = bodyParser.urlencoded ({Extended: false})/ / addedApp.post ('/process_post', urlencodedParser, function (req, RES) {//post processing method/ / output JSON formatVar response = {{"Names": req.body.nameS, / / get the data submitted by the page"Passwords": req.body.passwords};/ / linked databaseVar MYsql = require ("MySQL");Var connection = mysql.createConnection ({/ / configuration parameter), then add the table in your database.Host:'localhost',User:'root',Password:'root',Database:'test'})Connection.connect (); / / connection/ / insert dataVar addSql = "insert INto formtable (name, password) values (??); "/ / store database language, here is to addVar addParmas = [response.names,Response.passwords];Connection.query (addSql, addParmas, function (err, RES) {IF (ERR) {Console.log ("[insert error]-", err.message);Return;}else{Show (RES)}})Function show (ARR) {Result.data=arr;Res.json (result);Res.end ();}})/ / initializationApp.post ('/init', urlencodedParser, function (req, RES) {//post processing method/ / output JSON formatVar response = {{"Names": req.body.names / / get the data submitted by the page.};/ / linked databaseVar MySQL = require ("MySQL");Var connection = mysql.createConNection ({/ / configuration parameter, then add the table in your database.Host:'localhost',User:'root',PASsword:'root',Database:'test'})Connection.connect (); / / connectionVarSelectSql = "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 ();}})/ / delete dataApp.post ('/del', urlencodedParser, function (req, RES) {//post processing method/ / output JSON formatVar response = {{"IDS": req.body.id / / obtainedData submitted to the page};/ / linked databaseVar MySQL = require ("MySQL");Var connection =Mysql.createConnection ({/ / configuration parameter, then add the table in your database.Host:'localhost',User:'root',Password:'root',Database:'test'})Connection.connect (); // connectionVar 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 dataApp.post ('/modify', urlencodedParser, function (req, RES) {//post processing method/ / output JSON formatVar response = {{"IDS": req.body.ids, / / get the data submitted by the page."Names": req.body.names,"Passwords": req.body.passwords};/ / linkDatabaseVar MySQL = require ("MySQL");Var connection = mysql.createConnection ({/ / configuration parameter, and thenAdd the table in your databaseHost:'localhost',User:'root',Password:'root',Database:'test'})Connection.connect (); / / connectionVar userModSql ='UPDATE formtable SET name =?? password =? WHERE id =?Var addParmas = [response.names, responSe.passwords, response.ids];Console.log (addParmas);Connection.query (userModSql, a)DdParmas, 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 (3000, Fu)Nction () {/ / / / monitorVar host = server.address ().AddressVar port = server.address ().PortConsole.log ("application instance, access address is http://%s:%s", host, port)})}

  Writing this is mainly for my own learning. If there are good suggestions, you can exchange or enlighten me.

   Note: the formtable in the above code is only one of the tables in my local database, if your presentation is not modified in the MySQL statement that is introduced after the link to the database.

Leave a Reply

Your email address will not be published. Required fields are marked *