欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

nodejs之請求路由概述

 更新時間:2014年07月05日 17:42:04   投稿:shichen2014  
這篇文章主要介紹了nodejs的請求路由概述,需要的朋友可以參考下

通常來說對于不同的URL請求,服務器應該有不同的反應。我們要為路由提供請求的URL和其他需要的GET及POST參數(shù),隨后路由需要根據(jù)這些數(shù)據(jù)來執(zhí)行相應的代碼。我們需要的所有數(shù)據(jù)都會包含在request對象中,該對象作為onRequest()回調(diào)函數(shù)的第一個參數(shù)傳遞。為了解析這些數(shù)據(jù),需要調(diào)用額外的模塊,分別是url和querystring模塊。
 
URL:This
 module has utilities for URL resolution and parsing. Call require('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 of pathname and search.
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'
 
我們將使用依賴注入的方式較松散地添加路由模塊。作為路由目標的函數(shù)稱為請求處理程序,請求處理函數(shù)的實現(xiàn)需要創(chuàng)建一個叫做requestHandlers的模塊,當然也可以命名為其他。并對于每一個請求處理程序,添加一個占位用函數(shù),隨后將這些函數(shù)作為模塊的方法導出,這樣就可以將請求處理程序和路由模塊連接起來,讓路由有路可循。
 
特別指出的是,這里需要將一系列請求處理程序通過一個對象來傳遞,并且需要使用松耦合的方式將這個對象注入到route()函數(shù)中。

我們可以用從關(guān)聯(lián)數(shù)組中獲取元素一樣的方式從傳遞的對象中獲取請求處理函數(shù),因此就有了簡潔流暢的形如handle[pathname]();的表達式。代碼如下所示:

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

相關(guān)文章

  • 詳解nodejs如何實現(xiàn)查詢緩存

    詳解nodejs如何實現(xiàn)查詢緩存

    對于頻繁查詢、數(shù)據(jù)穩(wěn)定性高、讀取代價高的場景,查詢緩存可以發(fā)揮重要的作用,提高系統(tǒng)的性能和用戶體驗,下面我們就來學習一下nodejs是如何實現(xiàn)查詢緩存的
    2023-12-12
  • 使用Phantomjs和Node完成網(wǎng)頁的截屏快照的方法

    使用Phantomjs和Node完成網(wǎng)頁的截屏快照的方法

    這篇文章主要介紹了使用Phantomjs和Node完成網(wǎng)頁的截屏快照的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-07-07
  • 初識NodeJS服務端開發(fā)入門(Express+MySQL)

    初識NodeJS服務端開發(fā)入門(Express+MySQL)

    本篇文章主要介紹了初識NodeJS服務端開發(fā)入門(Express+MySQL),可以對數(shù)據(jù)庫中的一張表進行簡單的CRUD操作,有興趣的可以了解一下。
    2017-04-04
  • node.js事件循環(huán)機制及與js區(qū)別詳解

    node.js事件循環(huán)機制及與js區(qū)別詳解

    這篇文章主要為大家介紹了node.js事件循環(huán)機制及與js區(qū)別詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • 詳解從NodeJS搭建中間層再談前后端分離

    詳解從NodeJS搭建中間層再談前后端分離

    這篇文章主要介紹了詳解從NodeJS搭建中間層再談前后端分離,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • React和Node.js快速上傳進度條功能實現(xiàn)

    React和Node.js快速上傳進度條功能實現(xiàn)

    這篇文章主要為大家介紹了React和Node.js快速上傳進度條功能實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • node.js mongoose index索引操作

    node.js mongoose index索引操作

    在 Mongoose 中,索引(Index)是一種用于提高查詢性能的數(shù)據(jù)結(jié)構(gòu),它可以加速對數(shù)據(jù)庫中文檔的檢索操作,本文給大家介紹
    node.js mongoose index索引操作
    ,感興趣的朋友一起看看吧
    2023-12-12
  • node.js使用express框架進行文件上傳詳解

    node.js使用express框架進行文件上傳詳解

    在本篇內(nèi)容里小編給大家整理了關(guān)于node.js使用express框架進行文件上傳的相關(guān)知識點內(nèi)容,有需要的朋友們跟著學習下。
    2019-03-03
  • nodeJS中關(guān)于path.resolve()的用法解析

    nodeJS中關(guān)于path.resolve()的用法解析

    這篇文章主要介紹了nodeJS中關(guān)于path.resolve()的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Nodejs進階:核心模塊net入門學習與實例講解

    Nodejs進階:核心模塊net入門學習與實例講解

    本篇文章主要是介紹了Nodejs之NET模塊,net模塊是同樣是nodejs的核心模塊,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2016-11-11

最新評論