Node js路由

/*
要为程序提供请求的 URL 和其他需要的 GET 及 POST 参数,随后程序需要根据这些数据来执行相应的代码。
因此,需要查看 HTTP 请求,从中提取出请求的 URL 以及 GET/POST 参数。
需要的所有数据都会包含在 request 对象中,该对象作为 onRequest() 回调函数的第一个参数传递( onRequest()方法是 http.createServer()方法的参数,如下实例 )。但是为了解析这些数据,需要额外的 Node.JS 模块,分别是 url 和 querystring 模块。

                   url.parse(request.url).query
                                           |
           url.parse(request.url).pathname      |
                       |                   |
                       |                   |
                     ------ -------------------
http://localhost:8888/start?foo=bar&hello=world
                                ---       -----
                                 |          |
                                 |          |
              querystring.parse(queryString)["foo"]    |
                                            |
                         querystring.parse(queryString)["hello"]


也可以用 querystring 模块来解析 POST 请求体中的参数
*/
//===============================以下代码为main.js文件中内容==========================================
var server = require("./server");
var router = require("./router");
//调用server对象的start方法,并将router对象的route方法做为参数传过去
server.start(router.route);
//===============================以上代码为main.js文件中内容==========================================



//===============================以下代码为server.js文件中内容==========================================
var http = require("http");
var url  = require("url");

function start(route) {
    function onRequest(request, response) {
        /*
        request.url : 从域名端口号8888后的所有字符串,若地址结尾有/,则 request.url 值结尾也有。
        此例访问地址为 http://127.0.0.1:8888/start/work?foo=bar&hello=world/ 则 request.url 值为 /start/work?foo=bar&hello=world/

        打印url.parse(request.url);可以发现
        url.parse(request.url).pathname = /start/work
        url.parse(request.url).query    = foo=bar&hello=world/
        */

        var pathname = url.parse(request.url).pathname;
        var query    = url.parse(request.url).query;
        
        //这个判断如不加则会有两次请求,第二次请求是浏览器为获取收藏夹显示的图标而发送的
        /*
        if(pathname == '/favicon.ico'){
            response.end();
            return;
        }
        */
        console.log("pathname : " + pathname);
        console.log("query : " + query);
        //console.log(url.parse(request.url));
        //调用传过来的方法名,将值传入该方法并调用
        route(pathname);
        route(query);
        
        response.writeHead(200, {"Content-Type": "text/plain"});
        response.write("Hello World");
        response.end();
    }
    
    http.createServer(onRequest).listen(8888);
    console.log("Server has started.");
}
//start();
exports.start = start;
//===============================以上代码为server.js文件中内容==========================================



//===============================以下代码为router.js文件中内容==========================================

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

//===============================以上代码为router.js文件中内容==========================================


/*
执行main.js
node main.js
输出内容:
Server has started.
pathname : /start/work
query : foo=bar&hello=world/
About to route a request for /start/work
About to route a request for foo=bar&hello=world/
pathname : /favicon.ico
query : null
About to route a request for /favicon.ico
About to route a request for null

可以看到发送了两次请求,第二次的/favicon.ico是浏览器请求的,浏览器为在收藏夹中的显示图标.默认为favicon.ico.而自动发出的请求的目标URL地址
若想过滤掉该请求,可打开server.js中的if条件
*/

 

posted @ 2018-03-23 23:23  风吹屁股凉冰冰  阅读(186)  评论(0编辑  收藏  举报