nodejs之請(qǐng)求路由概述
通常來(lái)說(shuō)對(duì)于不同的URL請(qǐng)求,服務(wù)器應(yīng)該有不同的反應(yīng)。我們要為路由提供請(qǐng)求的URL和其他需要的GET及POST參數(shù),隨后路由需要根據(jù)這些數(shù)據(jù)來(lái)執(zhí)行相應(yīng)的代碼。我們需要的所有數(shù)據(jù)都會(huì)包含在request對(duì)象中,該對(duì)象作為onRequest()回調(diào)函數(shù)的第一個(gè)參數(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'
我們將使用依賴注入的方式較松散地添加路由模塊。作為路由目標(biāo)的函數(shù)稱為請(qǐng)求處理程序,請(qǐng)求處理函數(shù)的實(shí)現(xiàn)需要?jiǎng)?chuàng)建一個(gè)叫做requestHandlers的模塊,當(dāng)然也可以命名為其他。并對(duì)于每一個(gè)請(qǐng)求處理程序,添加一個(gè)占位用函數(shù),隨后將這些函數(shù)作為模塊的方法導(dǎo)出,這樣就可以將請(qǐng)求處理程序和路由模塊連接起來(lái),讓路由有路可循。
特別指出的是,這里需要將一系列請(qǐng)求處理程序通過(guò)一個(gè)對(duì)象來(lái)傳遞,并且需要使用松耦合的方式將這個(gè)對(duì)象注入到route()函數(shù)中。
我們可以用從關(guān)聯(lián)數(shù)組中獲取元素一樣的方式從傳遞的對(duì)象中獲取請(qǐng)求處理函數(shù),因此就有了簡(jiǎn)潔流暢的形如handle[pathname]();的表達(dá)式。代碼如下所示:
var handle = {}
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
相關(guān)文章
從零學(xué)習(xí)node.js之簡(jiǎn)易的網(wǎng)絡(luò)爬蟲(chóng)(四)
簡(jiǎn)單的爬蟲(chóng)實(shí)現(xiàn)原理很簡(jiǎn)單:發(fā)送http請(qǐng)求至目標(biāo)地址獲取HTML頁(yè)面數(shù)據(jù),然后從獲取來(lái)的頁(yè)面數(shù)據(jù)中提取需要的數(shù)據(jù)保存。下面這篇文章主要介紹了利用node.js實(shí)現(xiàn)簡(jiǎn)易的網(wǎng)絡(luò)爬蟲(chóng)的相關(guān)資料,需要的朋友可以參考下。2017-02-02
基于NodeJS的前后端分離的思考與實(shí)踐(二)模版探索
在傳統(tǒng)的開(kāi)發(fā)模式中,瀏覽器端與服務(wù)器端是由不同的前后端兩個(gè)團(tuán)隊(duì)開(kāi)發(fā),但是模版卻又在這兩者中間的模糊地帶。因此模版上面總不可避免的越來(lái)越多復(fù)雜邏輯,最終難以維護(hù)。2014-09-09
NodeJS設(shè)計(jì)模式總結(jié)【單例模式,適配器模式,裝飾模式,觀察者模式】
這篇文章主要介紹了NodeJS設(shè)計(jì)模式,結(jié)合實(shí)例形式總結(jié)分析了nodejs單例模式,適配器模式,裝飾模式,觀察者模式的概念、原理與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09
node.js中的fs.createReadStream方法使用說(shuō)明
這篇文章主要介紹了node.js中的fs.createReadStream方法使用說(shuō)明,本文介紹了fs.createReadStream方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下2014-12-12
使用async、enterproxy控制并發(fā)數(shù)量的方法詳解
并發(fā)相信對(duì)大家來(lái)說(shuō)都不陌生,這篇文章主要給大家介紹了關(guān)于使用async、enterproxy控制并發(fā)數(shù)量的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
基于node.js的fs核心模塊讀寫(xiě)文件操作(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇基于node.js的fs核心模塊讀寫(xiě)文件操作(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就想給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09

