三个文件:
index.js这是主文件:
server和router都是自定义的模块文件,调用route的时候参数必须是router.route。
var server=require("./module/server");var router=require("./module/router");server.start(router.route);
server.js服务器脚本文件:
var http = require("http");var url=require("url");function start(route) { function onRequest(request, response) { var pathname=url.parse(request.url).pathname; route(pathname); response.writeHead(200, { "Content-Type": "text/plain" }); response.write("hello,world"); response.end(); } http.createServer(onRequest).listen(8888);}exports.start=start;
router.js文件:
function route(pathname){ console.log("这是来自"+pathname+"的消息");}exports.route=route;