Node.js 参数的接收
GET方式接收参数
login.html
<html>
<head>
</head>
<body>
登录界面
<img src= "./showImg"/>
<form accept="./login" method="get">
<table align="center">
<tr>
<td>email:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td align="center"><input type="submit" name="login"></td>
</tr>
</table>
</form>
</body>
</html>
var http = require('http');
var url = require('url');
var router = require('./router');
http.createServer(function(request,response){
if(request.url != '/favicon.ico'){
var pathname = url.parse(request.url).pathname;
pathname = pathname.replace(/\//,''); //替换掉前面的'/'
console.log("Request for " + pathname + " received.");
try{
router[pathname](request,response);
}catch(err){
console.log("error:" + err);
response.writeHead(200,{'Content-Type' : 'text/html; charset=UTF-8'});
response.write(err.toString());
response.end('');
}
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000');
router.js
var optfile = require('./fs_read');
var url = require('url');
var querystring = require('querystring');
function getRecall(req,res){
function recall(data){
res.writeHead(200,{'Content-Type' : 'text/html; charset=UTF-8'});
res.write(data);
res.end('');
}
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']);
}
},
register : function(req,res){
recall = getRecall(req,res);
optfile.readfile('register.html',recall);
},
showImg : function(req,res){
res.writeHead(200,{'Content-Type' : 'image/jpeg'});
optfile.readImg('./1.png',res);
}
}
fs_read.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);
recall("文件不存在");
}else{
recall(data); //回调recall函数,它是闭包函数,它会存储原来的response对象
console.log(data.toString());
}
});
console.log("异步方法执行完毕");
},
readImg : function(path,res){
fs.readFile(path,'binary',function(err,file){
if(err){
console.log(err);
return ;
}else{
res.write(file,'binary');
res.end();
}
});
}
}
POST方式接收参数
login.html
<html>
<head>
</head>
<body>
登录界面
<img src= "./showImg"/>
<form accept="./login" method="post">
<table align="center">
<tr>
<td>email:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td align="center"><input type="submit" name="login"></td>
</tr>
</table>
</form>
</body>
</html>
var http = require('http');
var url = require('url');
var router = require('./router');
http.createServer(function(request,response){
if(request.url != '/favicon.ico'){
var pathname = url.parse(request.url).pathname;
pathname = pathname.replace(/\//,''); //替换掉前面的'/'
console.log("Request for " + pathname + " received.");
try{
router[pathname](request,response);
}catch(err){
console.log("error:" + err);
response.writeHead(200,{'Content-Type' : 'text/html; charset=UTF-8'});
response.write(err.toString());
response.end('');
}
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000');
router.js
var optfile = require('./fs_read');
var url = require('url');
var querystring = require('querystring');
function getRecall(req,res){
function recall(data){
res.writeHead(200,{'Content-Type' : 'text/html; charset=UTF-8'});
res.write(data);
res.end('');
}
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']);
}*/
//post方式接收参数
var post = ''; //定义了一个post变量,用于暂存请求体的信息
req.on('data',function(chunck){ //通过req的data事件监听,每当接收到请求体的数据,进行叠加
post += chunck;
});
//注意异步
req.on('end',function(){ //在end事件触发后,通过querystring.parse将post解析为真正的post信息
post = querystring.parse(post);
console.log('收到参数email:' + post['email'] + '\n');
console.log('收到参数password:' + post['pwd'] + '\n');
recall = getRecall(req,res);
optfile.readfile('login.html',recall);
});
},
register : function(req,res){
recall = getRecall(req,res);
optfile.readfile('register.html',recall);
},
showImg : function(req,res){
res.writeHead(200,{'Content-Type' : 'image/jpeg'});
optfile.readImg('./1.png',res);
}
}