GET方式接收參數(shù)
POST方式接收參數(shù)
代碼: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方式接收參數(shù)
console.log("嘗試GET方式獲接收參數(shù)");
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方式傳遞參數(shù)或未傳遞參數(shù)");
// post方式接收參數(shù)
var post = "";
console.log("嘗試POST方式接收參數(shù)");
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("未傳遞參數(shù)");
}
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("請(qǐng)求路徑:"+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("主程序執(zhí)行完畢");
}
}).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/
請(qǐng)求路徑:login
嘗試GET方式獲接收參數(shù)
POST方式傳遞參數(shù)或未傳遞參數(shù)
嘗試POST方式接收參數(shù)
主程序執(zhí)行完畢
未傳遞參數(shù)
異步讀文件完成
請(qǐng)求路徑:login
嘗試GET方式獲接收參數(shù)
GET方式接收email:fcj
GET方式接收pwd:123456
異步讀文件完成
主程序執(zhí)行完畢
請(qǐng)求路徑:login
嘗試GET方式獲接收參數(shù)
POST方式傳遞參數(shù)或未傳遞參數(shù)
嘗試POST方式接收參數(shù)
主程序執(zhí)行完畢
POST方式接收email:fcj
POST方式接收pwd:123456
異步讀文件完成
參考文檔
nodejs10_參數(shù)接收(n10_param)
http://www.yuankuwang.com/web/index.php?r=respool/resview&rpid=43
node.js教程10_參數(shù)接收
http://edu.51cto.com/center/course/lesson/index?id=124534