node.js学习笔记_模拟路由

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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‘:    ‘text技术分享ml;    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


本文出自 “南山采菊” 博客,请务必保留此出处http://hezudao.blog.51cto.com/6872139/1870817

以上是关于node.js学习笔记_模拟路由的主要内容,如果未能解决你的问题,请参考以下文章

node.js学习笔记_模块调用

node.js学习笔记之_读文件

vue+node.js学习笔记

node视频学习笔记入门笔记

weex开发学习笔记_环境搭建

系列文章--Node.js学习笔记系列