Nodejs use case-mysql action

Source: Internet
Author: User

1, Package.json:

{"  scripts": {    "start": "Node App.js"  },  "Devdependencies": {    "Body-parser": "^1.17.1",    " Ejs ":" ^2.5.6 ",    " Express ":" ^4.15.2 ",    " express-session ":" ^1.15.1 ",    " MySQL ":" ^2.13.0 ",    " session ": "^0.1.0"  }}

2, Db.js:

var mysql = require (' mysql ');//import MySQL module var db=mysql.createconnection ({//Connect MySQL database host: ' localhost ', User: ' Root ', Password: ' 123456 ', Database: ' Test ',  //DB name port:3306});d B.connect (Err) =>{if (err) {Console.log (' Connect Fail ')}else{console.log (' Connect suc ')}); module.exports=db;

3, App.js:

var express = require (' Express '), var app = Express (), var = require ('./modle.js '); var bodyparser = require (' Body-parse R '), var session = require (' express-session ') App.use (session ({secret: ' Shixinke ', resave:true,saveuninitialized: False,cookie: {Secure:false}}); This part of the code allows you to use Sessionapp.engine ('. html ', require (' Ejs '), __express), App.set (' View engine ', ' HTML '), App.set (' views '), __dirname + '/views ');  Template engine enabled App.use (Bodyparser.json ()); App.use (bodyparser.urlencoded ()); This section allows you to use the Receive post data app.get ('/', function (req, res, next) {///route, and then call the corresponding Modle.js method Gets.showarticlehome (req, res, Next)}) App.get ('/login ', function (req, res, next) {Res.render (' login ');}) App.get ('/outlogin ', function (req, res, next) {req.session.sign = False;res.redirect ('/');}) App.post ('/loginif ', function (req, res, next) {gets.loginif (req, res, next);}) App.set (' Trust proxy ', 1)//Trust First Proxyapp.get ('/list ', function (req, res, next) {if (req.session.sign) {Gets.showa Rticle (req, res, next)} else {res.send (' DoN,t has login ')}) app.get ('/article/:id ', function (req, res, next) {Gets.showarticleinfo (req, res, next)}) App.get ('/ Write ', function (req, res, next) {Gets.subform (req, res, next)}) app.post ('/subform ', function (req, res, next) { Gets.savearticle (req, res, next);}) App.get ('/delete/:id ', function (req, res, next) {gets.deletearticle (req, res, next);}) App.get ('/edit/:id ', function (req, res, next) {if (req.session.sign) {gets.editarticle (req, res, next);} Else{res.send (' You don,t has login in ')}) app.post ('/update ', function (req, res, next) {gets.updatearticle (req, res, Next);}) App.listen (8000)

4, Modle.js:

var db = require ('./db.js '); var gets = {};gets.loginif= (req,res,next) =>{var Userinfo=req.body.userinfo;console.log (UserInfo, ' sdflksjfd ') var qry= "SELECT COUNT (*) as num from admin where user= '" +userinfo.user+ "' and password= '" +userinfo . psd+ "'";d b.query (qry,function (Err,result) {if (err) {console.log (' err '); return;} if (result[0].num>=1) {req.session.sign = true;} Res.send (Result)})}gets.findall = (req, res, next) = {var qry = ' select * from user '; var qry2 = "Select * FROM User_ne XT ";d b.query (qry, function (err, result) {if (err) {console.log (' err '); return;} Db.query (Qry2, function (err, RESULT2) {res.render (' home ', {data:result,data2:result2,title: ' Home ')})});}  Gets.subform = (req, res, next) = {Res.render (' write ', {})}gets.showarticle = (req, res, next) = {db.query (' Select * from article ', function (err, result) {if (err) {Console.log (' not get article ') return;} Res.render (' list ', {Data:result})})}gets.showarticlehome = (req, res, next) = = {Db.query (' select * from article '), function (err, result) {if (err) {Console.log (' not get article ') return;} Res.render (' home ', {Data:result})})}gets.showarticleinfo = (req, res, next) = {var id = req.params.id;db.query ("Sele CT * from article where id= "+ ID +", function (err, result) {if (err) {Console.log (' not get article ') return; Res.render (' info ', {data:result})})}gets.savearticle = (req, res, next) = = {var Formdata = Req.body.formdata;console. Log (typeof (Formdata.tit)); var str = FORMDATA.CON;STR = str.replace (/\ "/g," "); str = str.replace (/"/g, "'"); str = STR.REPL Ace (/&/g, ""); str = str.replace (/lt;/g, "<"); str = str.replace (/gt;/g, ">"); str = str.replace (/\&/g, "");  str = str.replace (/\n/g, "<br>"), var qry = "INSERT into article (title,content) VALUES ('" + Formdata.tit + "', '" + str + "')";d b.query (qry, function (err, result) {if (err) {console.log (' err ') return;} Res.send ({state:true})})}gets.updatearticle = (req, res, next) = = {var Formdata = Req.body.formdata;var str = formdata . constr = str.replace (/\ "/g," "); str = str.replace (/"/g, "'"); str = str.replace (/&/g, ""); str = str.replace (/lt;/g, "&lt ;"); str = str.replace (/gt;/g, ">"); str = str.replace (/\&/g, ""); str = str.replace (/\n/g, "<br>"); var qry = "Updat E article set title= ' "+formdata.tit+" ', content= ' "+str+" ' Where id= ' "+req.body.id+" ' ";d b.query (qry, function (err, Result) {if (err) {console.log (' err ') return;} Res.send ({state:true})})}gets.deletearticle = (req, res, next) = = {var Delid=req.params.id;var qry= "Delete from Artic Le where id= "+delid+" ";d b.query (qry, function (err, result) {if (err) {console.log (' err ') return;} Res.redirect ('/list ')})}gets.editarticle = (req, res, next) = = {var editid=req.params.id;console.log (Editid, ' DKSFDKSFJ ') var qry= "select * from article where id=" +editid+ "";d b.query (qry, function (err, result) {if (err) {Console.log (' err ') return; Res.render (' edit ', {Data:result})})}module.exports = gets;

5, home.html:

<! DOCTYPE html>

6, Login.html:

<! DOCTYPE html>

  

Nodejs use case-mysql action

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.