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
posted @ 2016-11-08 21:23  老何de技术日志  阅读(294)  评论(0编辑  收藏  举报