Nodejs 动态访问路由
// d26_router_post.js
var url=require('url');
var optfile=require('./d26_optfile.js');
var querystring=require('querystring');//post方式需导入
function getRecall(rq, rs){
rs.writeHead(200, {'Content-Type':'text/html; charset=utf-8'});
function recall(data){
rs.write(data);
rs.end(''); //不可省略
}
return recall;
}
module.exports={
login:function(req, res){
//接收参数: post方式,定义post变量:暂存请求体的信息
var post=''; //以req的data事件监听,收到请求数据时就加到post变量
req.on('data', function(chunk){ post+=chunk; });
req.on('end',function(){ //注意:异步调用
post=querystring.parse(post);
var arr=['email', 'pwd'];
function recall(data){ //重写recal()回调用函数
var dataStr=data.toString();
for (var i = 0; i < arr.length; i++) {
var re=new RegExp('{'+arr[i]+'}','g'); //改写 /\{name\}/g
dataStr=dataStr.replace(re, post[arr[i]]);
}
res.write(dataStr);
res.end(''); //不可省略
}
optfile.readfileAsync('./views/d26_login_post.html', recall);
}); //end事件后,querystring.parse将post解析为正确post数据格式,再返回客户端
},
sign:function(req, res){
var recall=getRecall(req, res);
optfile.readfileAsync('./views/sign.html',recall);
},
writeFile: function(req, res){
var recall=getRecall(req, res);
optfile.writefileAsync('./views/one.txt','我写入的文件',recall)
},
showimg: function(req, res){
res.writeHead(200, {'Content-Type':'image/jpeg'});
optfile.readImg('./imgs/mn.jpeg',res);
}
}
d26_router_post.js 路由文件的重点难点:
var re=new RegExp(’{’+arr[i]+’}’,‘g’);
dataStr=dataStr.replace(re, post[arr[i]]);
HTML页面
html其它页面自己随便写,d26_login_post.html 重点页面示例代码如下:
<!-- d26_login_post.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>login</title>
</head>
<body>
<h3>动态网页: form表单数据提交</h3>
<img src="./showimg" />
<hr/>
收到参数 <br/>
email: {email} <br/>
密码: {pwd}
<hr/>
<form action="./login" method="post"> <!-- get 或 post -->
Email: <input type="text" name="email" /> <br/>
密码: <input type="password" name="pwd" /> <br/>
<input type="submit" value="登录" />
</form>
</body>
</html>
页面的动态访问
// d26_dynamic.js
var http=require('http');
var url=require('url');
var router=require('./models/d26_router_post.js');
http.createServer(function(request, response){
if(request.url!=='/favicon.ico'){ //清除2次访问
var pathname=url.parse(request.url).pathname;
pathname=pathname.replace(/\//,''); //替换(/)
try{//1、异常处理时,以try{}catch(){}实现事件处理;
router[pathname](request, response);
}catch(err){
console.log('错误提示1:'+err);
response.writeHead(200, {'Content-Type':'text/html; charset=utf-8'});
response.write('错误提示1:'+err.toString());
response.end('');
}
console.log('Server执行完毕');
}
}).listen(8000); //监听端口
console.log('Server running at http://127.0.0.1:8000/');
Nodejs 访问执行代码
// d26_optfile.js
var fs=require('fs'); //node.js自带fs文件操作类
module.exports={
readfileAsync: function(path, recall){ //异步执行
fs.readFile(path, function(err,data){
if(err){
console.log(path+' 地址访问错误:\n'+err);
recall('文件访问错误!');
//2 异步的异常处理时,系统自带异常捕获数据,可添加recall()回调函数处理;
}else{
console.log(data); //后端输出 data.toString()可转文本
recall(data);
}
});
console.log('异步方法执行完毕');
},
readImg: function(path, res){ //异步执行
fs.readFile(path,'binary', function(err,filedata){
if(err){
console.log(err);
return;
}else{
res.write(filedata, 'binary'); //二进制发送
res.end('');
}
});
},
writefileAsync: function(path, data, recall){ //异步写入
fs.writeFile(path, data, function(err){
if(err){throw err}
console.log('It\'s saved!'); //后端输出
recall('写文件成功');
});
console.log('异步方法执行完毕');
}
}