node.js学习笔记_模拟路由
路由就是浏览器输入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
版权声明:本文为pkutao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。