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

发布时间:2020-06-07 09:47:48 作者:pkutao
来源:网络 阅读:466

 
路由就是浏览器输入url地址,服务端根据对url地址的解析,访问对应的代码模块。
var    http    =    require('http');
var    url    =    require('url');
var    router    =    require('./router');
http.createServer(function    (request,    response)    {
        response.writeHead(200,    {'Content-Type':    'textnode.js学习笔记之模拟路由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("我是注册方法");
    }
}


教程学习地址:
node.js学习笔记之模拟路由http://study.163.com/course/introduction/1003228034.htm#/courseDetail


推荐阅读:
  1. ensp单臂路由学习笔记
  2. node.js学习笔记之写文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

js request node

上一篇:【问题】The coprocessor thread sto

下一篇:脚本自动化:每2分钟备份一个文件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》