nodejs的路由在函数那一节已经提到过,主要实现基础是通过字符串调用函数
主文件main.js:
var http = require('http');
var url = require('url');
var route = require('./route.js');
http.createServer(function(request,response){
response.writeHead(200,{'Content-Type':'text/html,charset=utf-8'});
if(request.url!=='favicon.ico'){
var pathname = url.parse(request.url).pathname;
pathname = pathname.replace(/\//,'');
route[pathname](request,response);
response.end();
}
}).listen(8000);
路由文件route.js:
module.exports={
regist:function(request,response){
response.write('this is regist');
},
login:function(request,response){
response.write('this is login ');
}
}
浏览器结果: