路由:
先建一个 router.js 文件,代码为:
module.exports = {
login : function(req, res){
res.write('登录');
},
regist : function(req, res){
res.write('注册');
}
};
var http = require("http");
var url = require("url");
var router = require("./router");
http.createServer(function(req, res) {
res.writeHead( 200 , {"Content-Type":"text/html;charset=utf-8"});
if (req.url !== '/favicon.ico') {//清除第二次访问
var pathName = url.parse(req.url).pathname;// 如果访问路径为:http://localhost:8000/regist,则pathName=‘/regist’
pathName = pathName.replace(/\//, '');// 去掉斜杠“/”
console.log(pathName);
router[pathName](req,res);
res.end();
}
}).listen(8000);
console.log('服务器 端口号:8000 启动了...');