express 學習筆記(一)路由

先導入express:javascript

var express = require('express'); var app = express();

1.路由方法:html

    getpostputheaddeleteoptionstracecopylockmkcolmovepurgepropfindproppatchunlockreportmkactivitycheckoutmergem-searchnotifysubscribeunsubscribepatchsearch, 和 connectjava

  經常使用的get,post正則表達式

app.get('/', function (req, res) { res.send('GET request to the homepage'); });
特殊的all方法

app.all() 是一個特殊的路由方法,沒有任何 HTTP 方法與其對應,它的做用是對於一個路徑上的全部請求加載中間件app.all()
app.all('/secret', function (req, res, next) { console.log('Accessing the secret section ...'); next(); // pass control to the next handler });
來自 「/secret」 的請求,無論使用 GET、POST、PUT、DELETE 或其餘任何http模塊支持的 HTTP 請求,句柄都會獲得執行。

2.路由路徑
路由路徑能夠是普通字符串,也能夠是正則表達式
普通路徑:
// 匹配根路徑的請求 app.get('/', function (req, res) { res.send('root'); }); // 匹配 /about 路徑的請求 app.get('/about', function (req, res) { res.send('about'); }); // 匹配 /random.text 路徑的請求 app.get('/random.text', function (req, res) { res.send('random.text'); });
// 匹配 acd 和 abcd app.get('/ab?cd', function(req, res) { res.send('ab?cd'); }); // 匹配 abcd、abbcd、abbbcd等 app.get('/ab+cd', function(req, res) { res.send('ab+cd'); }); // 匹配 abcd、abxcd、abRABDOMcd、ab123cd等 app.get('/ab*cd', function(req, res) { res.send('ab*cd'); }); // 匹配 /abe 和 /abcde app.get('/ab(cd)?e', function(req, res) { res.send('ab(cd)?e'); });

使用正則表達式的路由路徑示例:express


路由句柄
能夠爲請求處理提供多個回調函數,其行爲相似中間件。惟一的區別是這些回調函數有可能調用  方法而略過其餘路由回調函數。能夠利用該機制爲路由定義前提條件
// 匹配任何路徑中含有 a 的路徑: app.get(/a/, function(req, res) { res.send('/a/'); }); // 匹配 butterfly、dragonfly,不匹配 butterflyman、dragonfly man等 app.get(/.*fly$/, function(req, res) { res.send('/.*fly$/'); });next('route')

使用一個回調函數處理路由:json

app.get('/example/a', function (req, res) { res.send('Hello from A!'); }); 

使用多個回調函數處理路由(記得指定 next 對象):api

app.get('/example/b', function (req, res, next) { console.log('response will be sent by the next function ...'); next(); }, function (req, res) { res.send('Hello from B!'); }); 

使用回調函數數組處理路由:數組

var cb0 = function (req, res, next) { console.log('CB0'); next(); } var cb1 = function (req, res, next) { console.log('CB1'); next(); } var cb2 = function (req, res) { res.send('Hello from C!'); } app.get('/example/c', [cb0, cb1, cb2]); 

混合使用函數和函數數組處理路由:app



響應方法:
res.download() 提示下載文件。
res.end() 終結響應處理流程。
res.json()發送一個 JSON 格式的響應.
res.jsonp()發送一個支持 JSONP 的 JSON 格式的響應.
res.redirect()重定向請求。
res.render() 渲染視圖模板。
res.send()發送各類類型的響應。
res.sendFile()以八位字節流的形式發送文件。
res.sendStatus()設置響應狀態代碼,並將其以字符串形式做爲響應體的一部分發送。

express.Router
可以使用  類建立模塊化、可掛載的路由句柄。 實例是一個完整的中間件和路由系統,所以常稱其爲一個 「mini-app」。
var cb0 = function (req, res, next) { console.log('CB0'); next(); } var cb1 = function (req, res, next) { console.log('CB1'); next(); } app.get('/example/d', [cb0, cb1], function (req, res, next) { console.log('response will be sent by the next function ...'); next(); }, function (req, res) { res.send('Hello from D!'); });express.RouterRouter

在 app 目錄下建立名爲 birds.js 的文件,內容以下:dom

 
var express = require('express'); var router = express.Router(); // 該路由使用的中間件 router.use(function timeLog(req, res, next) { console.log('Time: ', Date.now()); next(); }); // 定義網站主頁的路由 router.get('/', function(req, res) { res.send('Birds home page'); }); // 定義 about 頁面的路由 router.get('/about', function(req, res) { res.send('About birds'); }); module.exports = router; 
 

而後在應用中加載路由模塊:

 
var birds = require('./birds'); ... app.use('/birds', birds);
相關文章
相關標籤/搜索