设为首页 收藏本站
查看: 690|回复: 0

[经验分享] nodejs笔记2——请求路由

[复制链接]

尚未签到

发表于 2017-2-21 12:36:05 | 显示全部楼层 |阅读模式
  对于不同的URL请求,服务器应该有不同的反应。我们要为路由提供请求的URL和其他需要的GET及POST参数,随后路由需要根据这些数据来执行相应的代码。我们需要的所有数据都会包含在request对象中,该对象作为onRequest()回调函数的第一个参数传递。为了解析这些数据,需要调用额外的模块,分别是url和querystring模块。
  URL:This
module has utilities for URL resolution and parsing. Callrequire('url')to
use it.




Parsed URL objects have some or all of the following fields, depending on whether or not they exist in the URL string. Any parts that are not in the URL string will not be in the parsed object. Examples are shown for the
URL

'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'



  • href: The full URL that was originally parsed. Both
    the protocol and host are lowercased.
    Example:'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'


  • protocol: The request protocol, lowercased.
    Example:'http:'


  • host: The full lowercased host portion of
    the URL, including port information.

    Example:'host.com:8080'


  • auth: The authentication information portion of a
    URL.
    Example:'user:pass'


  • hostname: Just the lowercased hostname portion of
    the host.
    Example:'host.com'


  • port: The port number portion of the host.
    Example:'8080'


  • pathname: The path section of the URL, that
    comes after the host and before the query, including the initial slash if present.

    Example:'/p/a/t/h'


  • search: The 'query string' portion of the URL, including
    the leading question mark.
    Example:'?query=string'


  • path: Concatenation ofpathnameandsearch.
    Example:'/p/a/t/h?query=string'


  • query: Either the 'params' portion of the query string,
    or a querystring-parsed object.
    Example:'query=string'or{'query':'string'}


  • hash: The 'fragment' portion of the URL including
    the pound-sign.
    Example:'#hash'



  我们将使用依赖注入的方式较松散地添加路由模块。作为路由目标的函数称为请求处理程序,请求处理函数的实现需要创建一个叫做requestHandlers的模块,当然也可以命名为其他。并对于每一个请求处理程序,添加一个占位用函数,随后将这些函数作为模块的方法导出,这样就可以将请求处理程序和路由模块连接起来,让路由有路可循。
  特别的是,将一系列请求处理程序通过一个对象来传递,并且需要使用松耦合的方式将这个对象注入到route()函数中。

我们可以用从关联数组中获取元素一样的方式从传递的对象中获取请求处理函数,因此就有了简洁流畅的形如handle[pathname]();的表达式。
  

var handle = {}
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload
;



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-345292-1-1.html 上篇帖子: nodejs http 发送请求 下篇帖子: nodejs生成npm包总结
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表