get 請求接收html
nj_param.jsnode
var http = require('http'); var url = require('url'); var router = require('./model/router'); http.createServer(function (request, response) { if(request.url!=="/favicon.ico"){ var pathname = url.parse(request.url).pathname;//獲取路徑名稱 pathname = pathname.replace(/\//,""); //正則去掉/ router[pathname](request,response);//根據路徑名稱獲取到函數從而調用函數 } }).listen(8000); console.log('Server running at http://127.0.0.1:8000/');
router.js異步
var openfile = require('./openfile'); var url = require('url'); //須要引入url function getRecall(req,res){ res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'}); function recall(data){ res.write(data); res.end('');//不寫則沒有http協議尾 } 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']); console.log(rdata['pwd']); } recall = getRecall(req,res); openfile.readfile('./view/login.html',recall); }, }
openfile.js函數
var fs= require('fs');//node自帶的類 module.exports={ readfile:function(path,recall){ //異步執行 fs.readFile(path, function (err, data) { if (err) { console.log(err); recall('文件不存在'); }else{ console.log(data.toString()); recall(data); } }); }, }
login.htmlpost
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <form action="./login" method="get"> <table> <tr> <td>email:</td> <td><input type="text" name="email"></td> </tr> <tr> <td>密碼:</td> <td><input type="password" name="pwd"></td> </tr> <tr> <td align="center"><input type="submit" value="登入"></td> </tr> </table> </form> </body> </html>
請求路徑http://localhost:8000/login 當輸入email和密碼的時候 點擊提交會再次請求login方法 router.js中ui
var rdata = url.parse(req.url,true).query;
會返回一個對象,打印對象中的屬性就能夠獲取參數
-----------------------------------------------------------------------------------------------------------
post提交
router.js
var openfile = require('./openfile'); var url = require('url'); var querystring = require('querystring'); //post需導入 function getRecall(req,res){ res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'}); function recall(data){ res.write(data); res.end('');//不寫則沒有http協議尾 } return recall; } module.exports={ login:function(req,res){ //-------post方式接收參數---------------- var post = ''; //定義了一個post變量,用於暫存請求體的信息 req.on('data', function(chunk){ //經過req的data事件監聽函數,每當接受到請求體的數據,就累加到post變量中 post += chunk; }); //-------注意異步------------- req.on('end', function(){ //在end事件觸發後,經過querystring.parse將post解析爲真正的POST請求格式,而後向客戶端返回。 post = querystring.parse(post); console.log('email:'+post['email']+'\n'); console.log('pwd:'+post['pwd']+'\n');
recall = getRecall(req,res);
openfile.readfile('./view/login.html',recall);
}); }, }
------------------------------------------------------------------------------------------------------------------------url
作一個動態的網頁參數的顯示 須要把表單的參數傳到前臺頁面中顯示出來orm
router.jsrouter
var openfile = require('./openfile'); var url = require('url'); var querystring = require('querystring'); //post需導入 function getRecall(req,res){ res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'}); function recall(data){ res.write(data); res.end('');//不寫則沒有http協議尾 } return recall; } module.exports={ login:function(req,res){ //-------post方式接收參數---------------- var post = ''; //定義了一個post變量,用於暫存請求體的信息 req.on('data', function(chunk){ //經過req的data事件監聽函數,每當接受到請求體的數據,就累加到post變量中 post += chunk; }); //-------注意異步------------- req.on('end', function(){ //在end事件觸發後,經過querystring.parse將post解析爲真正的POST請求格式,而後向客戶端返回。 post = querystring.parse(post); // recall = getRecall(req,res); arr = ['email','pwd']; function recall(data){//重寫了recall 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('');//不寫則沒有http協議尾 } openfile.readfile('./view/login.html',recall); }); }, }
login.htmlhtm
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> 收到參數:email是{email}<br> 密碼是:{pwd} <form action="./login" method="post"> <table> <tr> <td>email:</td> <td><input type="text" name="email"></td> </tr> <tr> <td>密碼:</td> <td><input type="password" name="pwd"></td> </tr> <tr> <td align="center"><input type="submit" value="登入"></td> </tr> </table> </form> </body> </html>