req.url:
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '',
query: {},
pathname: '/one',
path: '/one',
href: '/one' }
=====================get提交方法,接收参数
*******创建服务器
var http = require('http');
var fs = require('fs');
var url = require('url');
var routes=require('./models/routes');
http.createServer(function (request, response) {
var pathname = url.parse(request.url).pathname;
if(pathname!="/favicon.ico"){
pathname=pathname.replace(/\//,"");// 吧/去掉
try{
routes[pathname](request,response);//根据路径分发路由,执行相应的程序
}catch(err){
console.log("错误"+err);
response.writeHead(200,{"Content-Type":"text/html"});
response.write(err.toString());
response.end("");
}
console.log("主程序执行完毕");
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
********************route路由***********
var url = require('url');
url.parse(req.url,true).query;
var optfile=require("../models/optFile");
var url=require('url');
function getRecall(req,res){
res.writeHead(200, {'Content-Type': 'text/html'});
function recall(data){
res.write(data);// 发送响应数据,在网页上显示
res.end("ok");// 如果没有这句话,则网页一直在加载,不完成;就没有http协议尾部
}
return recall;
}
module.exports={
login:function(req,res){
var rdata=url.parse(req.url,true).query;//******************
console.log(rdata);
if(rdata["email"]!=undefined){
console.log(rdata['email']);
}
//创建一个闭包,作为参数传过去
recall=getRecall(req,res);
// optfile.readfileSync("D:nodejs/views/login.html");//同步读取文件
optfile.readfile("D:nodejs/views/login.html",recall);//异步读取文件
},
regist:function(req,res){
//创建一个闭包,作为参数传过去
recall=getRecall(req,res);
// optfile.readfileSync("D:nodejs/views/login.html");//同步读取文件
optfile.readfile("D:nodejs/views/regist.html",recall);//异步读取文件
},
//写文件
writeFile:function(req,res){
recall=getRecall(req,res);
optfile.writeFile("D:nodejs/views/write.text","写文件",recall);
},
readImage:function(req,res){
recall=getRecall(req,res);
optfile.readImage("D:nodejs/images/one.jpg",recall);
},
showImage:function(req,res){
res.writeHead(200, {'Content-Type': 'image/jpeg'});
optfile.readImage("D:nodejs/images/one.jpg",res);
}
}
********************************optfile.js具体执行代码
var fs=require('fs');//引入文件操作模块
module.exports={
readfileSync:function(path){
var data=fs.readFileSync(path,'utf-8');//同步方法读取文件
console.log(data);
console.log('同步方法执行完毕');
},
readfile:function(path,recall){
fs.readFile(path,function(err,data){
if(err){
console.log(err);
}else {
console.log(data.toString());
recall(data);
}
});
console.log("异步方法执行完毕");
},
//读取图片
readImage:function(path,res){
fs.readFile(path,"binary",function(err,data){
if(err){
console.log(err);
return;
}else {
res.write(data,"binary");
res.end("");
}
});
},
//异步写文件
writeFile:function (path,data,recall){
fs.writeFile(path,data,function(err){
if(err){
throw err;
}
console.log("savesuccess");
recall("写文件成功");
});
}
}
*******************************网页显示,表单提交;
<html>
<body>
<p>登陆</p>
<!-- <img src="./showImage"/> -->
<!-- 走到这的时候会去请求服务器,走路由里再去执行showImage方法-->
<form action="./login" method="get">
用户名<input type="text" name="email"/>
密码<input type="password" name="password"/>
<input type="submit" value="登陆"/>
</form>
</body>
</html>
==========================POST提交,接收参数,=========
var querystring = require('querystring');
module.exports={
login:function(req,res){
//post方式接受参数
var post='';//*************
req.on('data',function(chunk){//***************
post +=chunk;//****************
});//*******************
req.on('end',function(){//req的data事件监听//*****************
post=querystring.parse(post);//*********************
console.log('收到参数email:'+post['email']);//**************
});//***************************
//创建一个闭包,作为参数传过去
recall=getRecall(req,res);
// optfile.readfileSync("D:nodejs/views/login.html");//同步读取文件
optfile.readfile("D:nodejs/views/login.html",recall);//异步读取文件
}}