get 请求接收
nj_param.js
var http = require('http');
var url = require('url');
var router = require('./model/router');
http.createServer(function (request, response) {
if(request.url!=="/favicon.ico"){
var pathname = url.parse(request.url).pathname;//获取路径名称
pathname = pathname.replace(/\//,""); //正则去掉/
router[pathname](request,response);//根据路径名称获取到函数从而调用函数
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
router.js
var openfile = require('./openfile');
var url = require('url'); //需要引入url
function getRecall(req,res){
res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});
function recall(data){
res.write(data);
res.end('');//不写则没有http协议尾
}
return recall;
}
module.exports={
login:function(req,res){
//--------get方式接收参数----------------
var rdata = url.parse(req.url,true).query;
console.log(rdata);
if(rdata['email']!=undefined){
console.log(rdata['email']);
console.log(rdata['pwd']);
}
recall = getRecall(req,res);
openfile.readfile('./view/login.html',recall);
},
}
openfile.js
var fs= require('fs');//node自带的类
module.exports={
readfile:function(path,recall){ //异步执行
fs.readFile(path, function (err, data) {
if (err) {
console.log(err);
recall('文件不存在');
}else{
console.log(data.toString());
recall(data);
}
});
},
}
login.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <form action="./login" method="get"> <table> <tr> <td>email:</td> <td><input type="text" name="email"></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="pwd"></td> </tr> <tr> <td align="center"><input type="submit" value="登入"></td> </tr> </table> </form> </body> </html>
请求路径http://localhost:8000/login 当输入email和密码的时候 点击提交会再次请求login方法 router.js中
var rdata = url.parse(req.url,true).query;
会返回一个对象,打印对象中的属性就可以获取参数
-----------------------------------------------------------------------------------------------------------
post提交
router.js
var openfile = require('./openfile');
var url = require('url');
var querystring = require('querystring'); //post需导入
function getRecall(req,res){
res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});
function recall(data){
res.write(data);
res.end('');//不写则没有http协议尾
}
return recall;
}
module.exports={
login:function(req,res){
//-------post方式接收参数----------------
var post = ''; //定义了一个post变量,用于暂存请求体的信息
req.on('data', function(chunk){ //通过req的data事件监听函数,每当接受到请求体的数据,就累加到post变量中
post += chunk;
});
//-------注意异步-------------
req.on('end', function(){ //在end事件触发后,通过querystring.parse将post解析为真正的POST请求格式,然后向客户端返回。
post = querystring.parse(post);
console.log('email:'+post['email']+'\n');
console.log('pwd:'+post['pwd']+'\n');
recall = getRecall(req,res);
openfile.readfile('./view/login.html',recall);
}); }, }
------------------------------------------------------------------------------------------------------------------------
做一个动态的网页参数的显示 需要把表单的参数传到前台页面中显示出来
router.js
var openfile = require('./openfile');
var url = require('url');
var querystring = require('querystring'); //post需导入
function getRecall(req,res){
res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});
function recall(data){
res.write(data);
res.end('');//不写则没有http协议尾
}
return recall;
}
module.exports={
login:function(req,res){
//-------post方式接收参数----------------
var post = ''; //定义了一个post变量,用于暂存请求体的信息
req.on('data', function(chunk){ //通过req的data事件监听函数,每当接受到请求体的数据,就累加到post变量中
post += chunk;
});
//-------注意异步-------------
req.on('end', function(){ //在end事件触发后,通过querystring.parse将post解析为真正的POST请求格式,然后向客户端返回。
post = querystring.parse(post);
// recall = getRecall(req,res);
arr = ['email','pwd'];
function recall(data){//重写了recall
dataStr = data.toString(); //把参数转成字符串
for (var i = 0 ;i<arr.length; i++) {
re = new RegExp('{'+arr[i]+'}','g');
dataStr = dataStr.replace(re,post[arr[i]]); //正则替换了文本
}
res.write(dataStr);
res.end('');//不写则没有http协议尾
}
openfile.readfile('./view/login.html',recall);
});
},
}
login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
收到参数:email是{email}<br>
密码是:{pwd}
<form action="./login" method="post">
<table>
<tr>
<td>email:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td align="center"><input type="submit" value="登入"></td>
</tr>
</table>
</form>
</body>
</html>
