代码:https://github.com/fengchunjian/nodejs_examples/tree/master/appv1
//views/login.html
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
登录界面<br />
收到参数email: {email} <br />
pwd: {pwd} <br />
<form action="./login" method="get">
<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 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 = {
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"]);
arr = ["email", "pwd"];
function recall(data) {
dataStr = data.toString();
for (var i=0; i<arr.length; i++) {
re = new RegExp("{" + arr[i] + "}", "g");
dataStr = dataStr.replace(re, rdata[arr[i]]);
}
res.write(dataStr);
res.end();
}
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("未传递参数");
}
arr = ["email", "pwd"];
function recall(data) {
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();
}
optfile.readfile("./views/login.html", recall);
});
}
}
参考文档
node.js教程12_动态网页
http://edu.51cto.com/center/course/lesson/index?id=124537
nodejs12_动态网页(n12_dynamicpage)
http://www.yuankuwang.com/web/index.php?r=respool/resview&rpid=45