node.js的路由
作者:bin路由解析我理解就是解析http://domain/ 后面的具体path
这里用2个文件来说明,先弄一个解析path的方法, server.js
var http = require("http");
var url = require("url");
function start(route) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
route(pathname);
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
再用一个test.js来测试他
var Hello = require('./server');
Hello.start(function (path) {
console.log('在'+path);
})