当前位置:首页 >> 网络编程

nodejs学习笔记之路由

server.js

var http = require("http");
var url = require("url");
function start(route){//route.js 的route方法
  //服务实现
  function onRequest(request,response){
    var pathname = url.parse(request.url.).pathname;
    route(pathname); //
    
    response.writeHead(200,{"Content-Type":"text/plain"});
    response.write("只是一个练手罢了");
    response.end();
  }
  //创建服务
  http.createServer(onRequest).listen(8888);//端口号8888
  console.log("服务启动");//后台输出,表示服务启动成功
}
exports.start = start; //将start方法暴露出去

========================================

route.js

function route(pathname){
  console.log("请求路径是:" + pathname); //后台查看请求路劲  区分请求的地址
}
exports.route = route;

========================================

index.js

var server = require("./server");
var router = require("./route");
server.start(router.route);//传递給start的是router的route方法