router问题
var http = require("http"); var router = require("./router.js"); //创建服务器 var server = http.createServer(function(req,res){ if(req.url == "/"){ router.showIndex(req,res); }else if(req.url.substr(0,9) == "/student/"){ router.showStudent(req,res); }else{ router.show404(req,res); } }); server.listen(80,"127.0.0.1");
router.js
exports.showIndex = showIndex; exports.showStudent = showStudent; exports.show404 = show404; function showIndex(req,res){ res.writeHead(200,{"Content-Type":"text/html;charset=UTF8"}); res.end("我是首页"); } function showStudent(req,res){ var id = req.url.substr(9,6); res.writeHead(200,{"Content-Type":"text/html;charset=UTF8"}); res.end("我是学生页面" + id); } function show404(req,res){ res.writeHead(404,{"Content-Type":"text/html;charset=UTF8"}); res.end("404"); }