美文网首页
Day8 Node.js 路由

Day8 Node.js 路由

作者: Hathaway_桉 | 来源:发表于2017-06-08 13:20 被阅读0次

    server.js

    var http=require('http');
    var url=require('url');
    function start(route){
        function onRequest(request,response){
            var pathName=url.parse(request.url).pathname;
            console.log("request for"+pathname+"received");
            route(pathname);
            response.writeHead(200,{"Content-type":"text/plain"});
            response.write("hello world");
            response.end();
        }
        http.createServer(onRequest).listen(3000);
        console.log("Server has started");
    }
    exports.start=start;
    

    router.js

    function route(pathName){
        console.log("About to route a request for"+pathName);
    }
    exports.route=route;
    

    index.js

    var server=require("./server");
    var router=require("./router");
    server.start(router.route);
    

    相关文章

      网友评论

          本文标题:Day8 Node.js 路由

          本文链接:https://www.haomeiwen.com/subject/prozfxtx.html