路由就是浏览器输入url地址,服务端根据对url地址的解析,访问对应的代码模块。
varhttp=require('http');
varurl=require('url');
varrouter=require('./router');
http.createServer(function(request,response){
response.writeHead(200,{'Content-Type':'textml;charset=utf-8'});
if(request.url!=="/favicon.ico"){
varpathname=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('Serverrunningathttp://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