GET方式接收参数
POST方式接收参数
代码:https://github.com/fengchunjian/nodejs_examples/tree/master/appv0
//views/login.html
<html>
<head>
</head>
<body>
登录界面
<form action="./login" method="post">
<table align="center">
<tr>
<td>邮件:</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd" /></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
//models/router.js
var optfile = require("./optfile");
var exception = require("./Exception");
var url = require("url");
var querystring = require("querystring");
function getRecall(req, res) {
res.writeHead(200, {'Content-Type':'text/html;charset=utf-8'});
function recall(data) {
res.write(data);
res.end();
}
return recall;
}
module.exports = {
testexp : function(req, res) {
recall = getRecall(req, res);
try {
data = exception.expfun(0);
recall(data);
} catch(err) {
console.log("捕获自定义异常:"+err);
recall("捕获自定义异常:" + err.toString());
}
},
readImg : function(req, res) {
res.writeHead(200, {'Content-Type':'image/jpeg'});
optfile.readImg("./imgs/nodejs.jpg", res);
},
writefile : function(req, res) {
recall = getRecall(req, res);
optfile.writefile("./file.txt", "异步文件写入", recall);
},
login : function(req, res) {
// get方式接收参数
console.log("尝试GET方式获接收参数");
var rdata = url.parse(req.url, true).query;
if (rdata["email"] != undefined || rdata["pwd"] != undefined) {
console.log("GET方式接收email:" + rdata["email"]);
console.log("GET方式接收pwd:" + rdata["pwd"]);
recall = getRecall(req, res);
optfile.readfile("./views/login.html", recall);
return;
}
console.log("POST方式传递参数或未传递参数");
// post方式接收参数
var post = "";
console.log("尝试POST方式接收参数");
req.on("data", function(chunk) {
post += chunk;
});
req.on("end", function() {
post = querystring.parse(post);
if (post["email"] != undefined || post["pwd"] != undefined) {
console.log("POST方式接收email:" + post["email"]);
console.log("POST方式接收pwd:" + post["pwd"]);
} else {
console.log("未传递参数");
}
recall = getRecall(req, res);
optfile.readfile("./views/login.html", recall);
});
},
zhuce : function(req, res) {
recall = getRecall(req, res);
optfile.readfile("./views/zhuce.html", recall);
}
}
//app.js
var http = require('http');
var url = require('url');
var router = require('./models/router');
http.createServer(function (request, response) {
if(request.url!=="/favicon.ico") {
var pathname = url.parse(request.url).pathname;
pathname = pathname.replace(/\//, '');
try {
console.log("请求路径:"+pathname);
router[pathname](request, response);
} catch(err) {
console.log("同步捕获异常:"+err);
response.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});
response.write("同步捕获异常:"+err.toString());
response.end();
}
console.log("主程序执行完毕");
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
node app.js
Server running at http://127.0.0.1:8000/
请求路径:login
尝试GET方式获接收参数
POST方式传递参数或未传递参数
尝试POST方式接收参数
主程序执行完毕
未传递参数
异步读文件完成
请求路径:login
尝试GET方式获接收参数
GET方式接收email:fcj
GET方式接收pwd:123456
异步读文件完成
主程序执行完毕
请求路径:login
尝试GET方式获接收参数
POST方式传递参数或未传递参数
尝试POST方式接收参数
主程序执行完毕
POST方式接收email:fcj
POST方式接收pwd:123456
异步读文件完成
参考文档
nodejs10_参数接收(n10_param)
http://www.yuankuwang.com/web/index.php?r=respool/resview&rpid=43
node.js教程10_参数接收
http://edu.51cto.com/center/course/lesson/index?id=124534