1.接受參數:get 和 post html
login.html閉包
<form action="./login" method="get"><!--method=""--> <table> <tr> <td>姓名:</td> <td><input type="text" name="sex"/></td> </tr> <tr> <td>密碼:</td> <td><input type="password" name="password"/></td> </tr> <tr> <td></td> <td><button type="submit">提交</button></td> </tr> </table> </form>
router.js異步
var url = require('url');//引用url
var querystring = require('querystring'); //post需導入
module.exports={//路由 login:function(req,res){ //----get方式接受參數---- var rdata = url.parse(req.url,true).query; console.log(rdata); if(rdata['sex']!=undefined){ console.log(rdata['sex']); console.log(rdata['password']); }; //-------post方式接收參數---------------- var post="";//定義了一個post變量,用於暫存請求體的信息; req.on('data',function(chunk){ post += chunk;//經過req的data事件監聽函數,每當接受到請求體的數據,就累加到post變量中; }); //-------注意異步------------- req.on('end',function(){//在end事件觸發後,經過querystring.parse將post解析爲真正的post請求格式,而後向客戶端返回; post = querystring.parse(post); console.log('sex:'+post['sex']+'\n'); console.log('password:'+post['password']+'\n') }); //採用閉包; recall = abc(req,res); readfile.readfile('./views/login.html',recall); } }