Node js路由解析
沉沙 2018-05-15 来源 : 阅读 650 评论 0

摘要:查看 HTTP 请求,从中提取出请求的 URL 以及 GET/POST 参数。需要的所有数据都会包含在 request 对象中,该对象作为 onRequest() 回调函数的第一个参数传递( onRequest()方法是 http.createServer()方法的参数 。但是为了解析这些数据,需要额外的 Node js模块,分别是 url 和 querystring 模块。

要为程序提供请求的 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      |
                       |                   |
                       |                   |
                     ------ -------------------
//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);

 

 

以下代码为server.js文件中内容

var http = require("http");var url  = require("url");
function start(route) {
    function onRequest(request, response) {
        /*

        request.url : 从域名端口号8888后的所有字符串,若地址结尾有/,则 request.url 值结尾也有。

        此例访问地址为 //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;

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

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


 

/*

执行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条件

 

本文由职坐标整理发布,更多相关知识,请关注职坐标WEB前端Node.js频道!

本文由 @沉沙 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程