大佬教程收集整理的这篇文章主要介绍了Node.JS入门(五)——后台接收页面参数并保存到数据库,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在学习了nodejs和数据库MysqL交互的情况下,我们来学习一下如何从页面上获取参数然后存放到数据库中,这种方式通常有两种,一种是get方式,一种是post方式
背景
本篇的主要场景是获取注册中的用户名和密码,然后保存到数据库中的操作,其中保存的数据库是MysqL数据库,第一步就需要下载MysqL模块,关于连接MysqL连接池的内容详情请看上一篇文章:
node.js入门(四)——对mysql数据库的CRUD
开始
var MysqL=require('MysqL');
function OptPool(){
this.falg=true; //是否连接过
this.pool=MysqL.createPool({
host:'127.0.0.1',user:'root',password:'1',database:'test',port:'3306'
});
this.getPool=function(){
if(this.falg){
//监听connection事件
this.pool.on('connection',function(connection){
connection.query('SELEct session auto_increment_increment');
this.falg=false;
})
}
return this.pool;
}
}
module.exports=OptPool;
var OptPool=require('./OptPool');
var optPool=new OptPool();
var pool=optPool.getPool();
var fs=require('fs');
var queryString=require('queryString');
function insertUser(data) {
if(data.uname!==undefined&&data.pwd!==undefined){
pool.getConnection(function(err,conn){
if(err){
console.log(err);
}
//插入方法
var userAddsql='insert into user(uname,pwd) values(?,?)';
var param=[data.uname,data.pwd];
conn.query(userAddsql,param,function(err,rs){
if(err){
console.log('insert err',err.messagE);
return;
}
console.log('insert success');
});
})
}
}
function getRecall(req,res){
res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});
function recall(data){
res.write(data);
res.end('');
}
return recall;
}
function readFile(path,recall) {
fs.readFile(path,data){
if(err){
console.log(err);
}else{
console.log(data.toString());
recall(data);
}
});
console.log("异步方法执行完毕");
}
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="UTF-8">
<title>注册界面</title>
</head>
<body>
<form action="./zhuCe" @H_528_218@method="get">
<table align="center">
<tr>
<td>username</td>
<td><input type="text" name="uname"></td>
</tr>
<tr>
<td>password</td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td align="center"><input type="submit" value="注册"></td>
</tr>
</table>
</form>
</body>
</html>
这样在页面上面输入参数,并且点击注册方式之后,显示的页面效果如下
var url=require('url')
module.exports={
zhuCe:function (req,res) {
var recall=getRecall(req,res);
readFile('./file/zhuce.html',recall);//读取html文件
//--get接收参数
var data=url.parse(req.url,true).query;
//收到参数,向数据库中添加!
insertUser(data)//insertUser方法已经在前面介绍过,是保存数据的方法
})
}
}
POST参数接收
主要把上诉的html页面的method的方式改成post方式即可,页面输入参数之后,并且点击注册方法后,url的上面并没有出现参数,
req.on(‘data’,function(chunk){//通过req的data事件监听函数,每当接受到请求体的数据,就累加到post变量中
post += chunk;
var queryString=require('queryString')
module.exports={
zhuCe:function (req,res) {
//post提交方法
var post=''; //定义了一个post变量,用于暂时存放请求头
req.on('data',function (chunk) {
post+=chunk;
})
//--注意异步方式
req.on('end',function () {
var recall=getRecall(req,res);
readFile('./file/zhuce.html',recall);
post=queryString.parse(post);
req.write('收到参数'+data['uname'+'\\n'])
req.end();
insertUser(post)
})
}
}
var url=require('url')
var http =require('http')
var router=require('./module/MysqLpool');//该文件就是上诉写接收参数的文件
http.createServer(function (request,responsE) {
if(request.url!=='/favicon.ico'){
var pathname=url.parse(request.url).pathname//在浏览器中地址写入http://localhost:8888/zhuCe,截取到/zhuCe
pathname=pathname.replace(/\//,'');//获取zhuCe
console.log(pathName);
try{
router[pathname](request,responsE);//通过router[zhuCe]去执行注册方法
}catch(err){
console.log('出错'+err);
response.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});
response.write(err);
response.end();
}
console.log('serve执行完毕');
}
}).listen(8888);
@H_616_564@总结
后台接收前台页面参数的方式主要就是get和post的方式,两者主要的区别不仅在于URL地址参数的有无,还在于传递的方式,post采用的流的方式,接收参数的时候需要累加到变量。
以上是大佬教程为你收集整理的Node.JS入门(五)——后台接收页面参数并保存到数据库全部内容,希望文章能够帮你解决Node.JS入门(五)——后台接收页面参数并保存到数据库所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。