nodejs 构建一个简单的路由
//16.1.11.2.js
//http 服务端
var http = require('http');
var url = require("url");
function start(route) {
function onRequest(request, response) {
response.writeHead(200, {
"Content-Type": "text/plain"
});
//path
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received."); //服务端输出
//route输出
route(pathname);
response.write("Hello World!");
response.end();
}
http.createServer(onRequest).listen(8888);
}
exports.start = start;
//16.1.11.2_1.js
function route(pathname) {
console.log("About to route a request for " + pathname);
}
exports.route = route;
//16.1.11.2_2.js
var server = require('./16.1.11.2.js');
var router = require('./16.1.11.2_1.js')
server.start(router.route)