路由就是瀏覽器輸入url地址,服務端根據對url地址的解析,訪問對應的代碼模塊。
var http = require('http');
var url = require('url');
var router = require('./router');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'textml; charset=utf-8'});
if(request.url!=="/favicon.ico"){
var pathname = url.parse(request.url).pathname;
//console.log(pathname);
pathname = pathname.replace(/\//, '');//替換掉前面的/
//console.log(pathname); //輸出要調用的方法名,從url中解析出來
router[pathname](request,response);
response.end('');
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
//-----------------router.js--------------------------------
module.exports={
login:function(req,res){
res.write("我是login方法");
},
zhuce:function(req,res){
res.write("我是注冊方法");
}
}
教程學習地址:http://study.163.com/course/introduction/1003228034.htm#/courseDetail
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。