代码:https://github.com/fengchunjian/nodejs_examples/tree/master/router
\\vim models/router.js
module.exports = {
login : function(req, res) {
res.write("我是login方法");
},
zhuce : function(req, res) {
res.write("我是注册方法");
}
}
\\vim routercall.js
var http = require('http');
var url = require('url');
var router = require('./models/router');
http.createServer(function (request, response) {
var pathname = url.parse(request.url).pathname;
pathname = pathname.replace(/\//, '');
router[pathname](request, response);
response.end();
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
node routercall.js
Server running at http://127.0.0.1:8000/
curl http://127.0.0.1:8000/login
我是login方法
curl http://127.0.0.1:8000/zhuce
我是注册方法
参考文档
nodejs4_路由(n4_rout)
http://www.yuankuwang.com/web/index.php?r=respool/resview&rpid=36
node.js教程4_路由初步
http://edu.51cto.com/center/course/lesson/index?id=124528