张家港设计网站,网站及推广,宁国新站seo,还是新能源专业好Nodejs Web应用基础演示实例 
Web数据库应用 
一、服务器端 
var express  require(express);
var app  express();
var mysql  require(mysql);//设置静态资源目录public
app.use(express.static(__dirname  /public));//创建mysql数据库访问连接#xff08;数据库主机地址 require(express);
var app  express();
var mysql  require(mysql);//设置静态资源目录public
app.use(express.static(__dirname  /public));//创建mysql数据库访问连接数据库主机地址用户名和密码数据库名称根据情况自行修改
var connection  mysql.createConnection({host: localhost,user: root,password: 123456,database: test
});connection.connect();//允许跨域访问可以从不同主机进行调用
app.all(*,function (req, res, next) {res.header(Access-Control-Allow-Origin, *);res.header(Access-Control-Allow-Headers, Accept,Content-Type,Content-Length, Authorization,X-Requested-With );res.header(Access-Control-Allow-Methods, POST,GET,PUT,DELETE,OPTIONS);if (OPTIONS  req.method) {res.send(200); //让options请求快速返回}else {next();}
});//查询全部记录的API
app.get(/all, function(req, res) {connection.query(SELECT * from stu, function(error, results, fields) {//var list  ;if (error) throw error;//console.log(The solution is: , results);//list  JSON.stringify(results);//res.send(list);res.send(results);});});//按用户名进行查询的API
//前端发来的用户请求应该为“主机名:端口号/list?username*** ”
app.get(/list, function(req, res) {var usernamereq.query.username;connection.query(SELECT * from stu where username username, function(error, results, fields) {//var list  ;if (error) throw error;//console.log(The solution is: , results);//list  JSON.stringify(results);//res.send(list);res.send(results);});});//用户注册API
//前端发来的请求应该为“主机名:端口号/add?username***pwd***name***age*** ”
app.get(/add, function(req, res) {var usernamereq.query.username.trim();var pwdreq.query.pwd;var namereq.query.name;var ageparseInt(req.query.age);if(username){res.send({success:0, insertid:0, msg:用户名不能为空});return;}//查询是否存在同名用户connection.query(SELECT * from stu where username username, function(error, results, fields) {//查询出错则返回if (error){console.log([QUERY ERROR] - ,err.message);res.send({success:0, insertid:0, msg:操作异常});return;}//if (results.length0){var addSqlINSERT INTO stu(username,password,name,age) VALUES(?,?,?,?);var addSqlParams  [username, pwd, name, age];connection.query(addSql,addSqlParams,function (err, result) {if(err){console.log([INSERT ERROR] - ,err.message);res.send({success:0, insertid:0, msg:操作异常});return;}        console.log(--------------------------INSERT----------------------------);console.log(INSERT ID:,result.insertId);        console.log(-----------------------------------------------------------------\n\n);  res.send({success:1, insertid:result.insertId, msg:操作成功});});}else{res.send({success:0, insertid:0, msg:用户名已存在请重新注册});}});});var server  app.listen(8080, localhost,function() {var host  server.address().address;var port  server.address().port;console.log(应用实例访问地址为 http://%s:%s, host, port);})二、前端 
1. 用户查询页面 
htmlheadmeta charsetutf-8title根据用户请求查询后台数据库记录/title!--解决node提示favicon.ico图标文件不存在的问题--link relshortcut icon href# /script srchttps://cdn.staticfile.org/jquery/1.10.2/jquery.min.js/scriptscript$(document).ready(function() {//在前端页面容器中添加查询到的记录数据function show(record){var p0$(p classdata/p).html(IDspanrecord.id/span);$(#box).append(p0);var p1$(p classdata/p).text(姓名record.name);$(#box).append(p1);					var p2$(p classdata/p).text(年龄record.age);$(#box).append(p2);var p3$(p classdata/p).text(用户名record.username);$(#box).append(p3);$(#box).append(hr);};//查询数据库全部记录$(#all).click(function(){$(#box).empty();$.get(http://127.0.0.1:8080/all,function(data){//var recordsJSON.parse(data);for(var i0;idata.length;i){console.log(data[i]);show(data[i]);}});});//根据用户输入的用户名查询数据库中的特定记录								$(#list).click(function(){$(#box).empty();var username  $(#username).val();$.get(http://127.0.0.1:8080/list,{username:username},function(data){for(var i0;idata.length;i){console.log(data[i]);show(data[i]);}});});$(.data).click(function(){$(#username).val($(this).text());});});/script/headbodyp用户名: input typetext idusername value/pbutton idlist查询/buttonbutton idall查询全部/buttondiv idbox/div/body
/html2. 用户注册页面 
html
headtitleMySQL添加记录实例/titlemeta charsetutf-8meta nameviewport contentwidthdevice-width, initial-scale1link hrefhttps://cdn.staticfile.org/twitter-bootstrap/5.1.1/css/bootstrap.min.css relstylesheetscript srchttps://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js/scriptlink relshortcut icon href# /script srchttps://cdn.staticfile.org/jquery/1.10.2/jquery.min.js/scriptscript$(document).ready(function() {//清空输入框内容$(#clear).click(function(){$(#username).val();$(#pwd).val();$(#name).val();$(#age).val();});//根据用户输入的用户名查询数据库中的特定记录								$(#add).click(function(){var username  $(#username).val();var pwd  $(#pwd).val();var name  $(#name).val();var age  parseInt($(#age).val());$.get(http://127.0.0.1:8080/add,{username:username,pwd:pwd,name:name,age:age				},function(data){$(#box).text(data.msg);});});				});/script
/head
bodydiv classcontainer mt-3h2注册用户/h2div classmb-3 mt-3label forusername classform-label用户名:/labelinput typetext classform-control idusername placeholder输入用户名字母数字组合 nameusername/divdiv classmb-3label forpwd classform-label口令:/labelinput typepassword classform-control idpwd placeholder输入口令字母数字组合 namepwd/divdiv classmb-3label forname classform-label姓名/labelinput classform-control typetext idname namename /divdiv classmb-3label forage classform-label年龄/labelinput classform-control typetext idage nameage /divbutton idadd classbtn btn-primary提交/buttonbutton idclear classbtn btn-primary清除/buttondiv idbox/div
/div/body
/html
 文章转载自: http://www.morning.qkqzm.cn.gov.cn.qkqzm.cn http://www.morning.bwkzn.cn.gov.cn.bwkzn.cn http://www.morning.ftnhr.cn.gov.cn.ftnhr.cn http://www.morning.wphfl.cn.gov.cn.wphfl.cn http://www.morning.ygmw.cn.gov.cn.ygmw.cn http://www.morning.wbhzr.cn.gov.cn.wbhzr.cn http://www.morning.chfxz.cn.gov.cn.chfxz.cn http://www.morning.hkgcx.cn.gov.cn.hkgcx.cn http://www.morning.lwyqd.cn.gov.cn.lwyqd.cn http://www.morning.qlznd.cn.gov.cn.qlznd.cn http://www.morning.nbmyg.cn.gov.cn.nbmyg.cn http://www.morning.mjjty.cn.gov.cn.mjjty.cn http://www.morning.rpdmj.cn.gov.cn.rpdmj.cn http://www.morning.xqndf.cn.gov.cn.xqndf.cn http://www.morning.zcsch.cn.gov.cn.zcsch.cn http://www.morning.xsbhg.cn.gov.cn.xsbhg.cn http://www.morning.msgrq.cn.gov.cn.msgrq.cn http://www.morning.zknxh.cn.gov.cn.zknxh.cn http://www.morning.wschl.cn.gov.cn.wschl.cn http://www.morning.fbpdp.cn.gov.cn.fbpdp.cn http://www.morning.ltspm.cn.gov.cn.ltspm.cn http://www.morning.sdkaiyu.com.gov.cn.sdkaiyu.com http://www.morning.hqwxm.cn.gov.cn.hqwxm.cn http://www.morning.ndxss.cn.gov.cn.ndxss.cn http://www.morning.xxiobql.cn.gov.cn.xxiobql.cn http://www.morning.sqtsl.cn.gov.cn.sqtsl.cn http://www.morning.zdmlt.cn.gov.cn.zdmlt.cn http://www.morning.snkry.cn.gov.cn.snkry.cn http://www.morning.byrlg.cn.gov.cn.byrlg.cn http://www.morning.dkgtr.cn.gov.cn.dkgtr.cn http://www.morning.fjscr.cn.gov.cn.fjscr.cn http://www.morning.rknsp.cn.gov.cn.rknsp.cn http://www.morning.chmkt.cn.gov.cn.chmkt.cn http://www.morning.dnqliv.cn.gov.cn.dnqliv.cn http://www.morning.fgsqz.cn.gov.cn.fgsqz.cn http://www.morning.hsjrk.cn.gov.cn.hsjrk.cn http://www.morning.rkfwr.cn.gov.cn.rkfwr.cn http://www.morning.zdhnm.cn.gov.cn.zdhnm.cn http://www.morning.qbkw.cn.gov.cn.qbkw.cn http://www.morning.fnzbx.cn.gov.cn.fnzbx.cn http://www.morning.yrdn.cn.gov.cn.yrdn.cn http://www.morning.tqbqb.cn.gov.cn.tqbqb.cn http://www.morning.kkysz.cn.gov.cn.kkysz.cn http://www.morning.ccphj.cn.gov.cn.ccphj.cn http://www.morning.tstwx.cn.gov.cn.tstwx.cn http://www.morning.kdxzy.cn.gov.cn.kdxzy.cn http://www.morning.clndl.cn.gov.cn.clndl.cn http://www.morning.wjtwn.cn.gov.cn.wjtwn.cn http://www.morning.lmdfj.cn.gov.cn.lmdfj.cn http://www.morning.bqnhh.cn.gov.cn.bqnhh.cn http://www.morning.rnqbn.cn.gov.cn.rnqbn.cn http://www.morning.bhznl.cn.gov.cn.bhznl.cn http://www.morning.kscwt.cn.gov.cn.kscwt.cn http://www.morning.wbqt.cn.gov.cn.wbqt.cn http://www.morning.gjssk.cn.gov.cn.gjssk.cn http://www.morning.zshuhd015.cn.gov.cn.zshuhd015.cn http://www.morning.lhzqn.cn.gov.cn.lhzqn.cn http://www.morning.yqsr.cn.gov.cn.yqsr.cn http://www.morning.hbqfh.cn.gov.cn.hbqfh.cn http://www.morning.rzmzm.cn.gov.cn.rzmzm.cn http://www.morning.ie-comm.com.gov.cn.ie-comm.com http://www.morning.skwwj.cn.gov.cn.skwwj.cn http://www.morning.wlstn.cn.gov.cn.wlstn.cn http://www.morning.bxsgl.cn.gov.cn.bxsgl.cn http://www.morning.jmmz.cn.gov.cn.jmmz.cn http://www.morning.trlhc.cn.gov.cn.trlhc.cn http://www.morning.fjntg.cn.gov.cn.fjntg.cn http://www.morning.lstmg.cn.gov.cn.lstmg.cn http://www.morning.xskbr.cn.gov.cn.xskbr.cn http://www.morning.kghhl.cn.gov.cn.kghhl.cn http://www.morning.fwrr.cn.gov.cn.fwrr.cn http://www.morning.kjtdy.cn.gov.cn.kjtdy.cn http://www.morning.lrylj.cn.gov.cn.lrylj.cn http://www.morning.nqbcj.cn.gov.cn.nqbcj.cn http://www.morning.khpgd.cn.gov.cn.khpgd.cn http://www.morning.rtlrz.cn.gov.cn.rtlrz.cn http://www.morning.rkbly.cn.gov.cn.rkbly.cn http://www.morning.mkfr.cn.gov.cn.mkfr.cn http://www.morning.xtqr.cn.gov.cn.xtqr.cn http://www.morning.eshixi.com.gov.cn.eshixi.com