2、第一個 Express 框架實例

目標

使用 Express 框架,當在瀏覽器中訪問 http://localhost:3000/ 時,輸出 Hello Worldnode

安裝express

$ npm install express

新建一個 app.js 文件正則表達式

// 這句的意思就是引入 `express` 模塊,並將它賦予 `express` 這個變量等待使用。
var express = require('express');
// 調用 express 實例,它是一個函數,不帶參數調用時,會返回一個 express 實例,將這個變量賦予 app 變量。
var app = express();

// app 自己有不少方法,其中包括最經常使用的 get、post、put/patch、delete,在這裏咱們調用其中的 get 方法,爲咱們的 `/` 路徑指定一個 handler 函數。
// 這個 handler 函數會接收 req 和 res 兩個對象,他們分別是請求的 request 和 response。
// request 中包含了瀏覽器傳來的各類信息,好比 query 啊,body 啊,headers 啊之類的,均可以經過 req 對象訪問到。
// res 對象,咱們通常不從裏面取信息,而是經過它來定製咱們向瀏覽器輸出的信息,好比 header 信息,好比想要向瀏覽器輸出的內容。這裏咱們調用了它的 #send 方法,向瀏覽器輸出一個字符串。
app.get('/', function (req, res) {
    res.send('Hello World');
});

// 定義好咱們 app 的行爲以後,讓它監聽本地的 3000 端口。這裏的第二個函數是個回調函數,會在 listen 動做成功後執行,咱們這裏執行了一個命令行輸出操做,告訴咱們監聽動做已完成。
app.listen(3000, function () {
    console.log('app is listening at port 3000');
});

執行express

$ node app.js

這時候咱們的 app 就跑起來了,終端中會輸出 app is listening at port 3000。這時咱們打開瀏覽器,訪問 http://localhost:3000/,會出現 Hello World。npm

Express框架

載入express 而後使用代碼 express()建立一個新的應用程序:json

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

路由方法

路由方法派生自 HTTP 方法之一,附加到 express 類的實例。瀏覽器

如下代碼是爲訪問應用程序根目錄的 GET 和 POST 方法定義的路由示例。app

// GET method route
app.get('/', function (req, res) {
  res.send('GET request to the homepage');
});

// POST method route —— 根據請求路徑來處理客戶端發出的Post請求。
app.post('/', function (req, res) {
  res.send('POST request to the homepage');
});

Express 支持對應於 HTTP 方法的如下路由方法:get、post、put、head、delete、options、trace、copy、lock、mkcol、move、purge、propfind、proppatch、unlock、report、mkactivity、checkout、merge、m-search、notify、subscribe、unsubscribe、patch、search 和 connect。框架

路由路徑

路由路徑與請求方法相結合,用於定義能夠在其中提出請求的端點。路由路徑能夠是字符串、字符串模式或正則表達式。dom

基於字符串的路由路徑

此路由路徑將請求與根路由 / 匹配。函數

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');
});
基於正則表達式的路由路徑

此路由路徑將匹配名稱中具備「a」的全部路由。

app.get(/a/, function(req, res) {
  res.send('/a/');
});

此路由路徑將匹配全部以fly結尾的字符串 xfly、 butterfly,可是不匹配 butterflyman、dragonfly man 等。

app.get(/.*fly$/, function(req, res) {
  res.send('/.*fly$/');
});

路由處理程序

單個回調函數能夠處理一個路由

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

多個回調函數能夠處理一個路由(確保您指定 next 對象)

app.get('/x/b', function (req, res, next) {
  console.log('the response will be sent by the next function ...');
  next();
  console.log('111'); // next()執行完以後再打印
}, function (req, res) {
  console.log('222');
  res.send('Hello from B!');
  console.log('333');
});

獨立函數與一組函數的組合能夠處理一個路由。例如:

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('the response will be sent by the next function ...');
  next();
}, function (req, res) {
  res.send('Hello from D!');
});

響應方法

下表中響應對象 (res) 的方法能夠向客戶機發送響應,並終止請求/響應循環。若是沒有從路由處理程序調用其中任何方法,客戶機請求將保持掛起狀態。

方法 描述
res.download() 提示將要下載文件
res.end() 結束響應進程
res.json() 發送 JSON 響應
res.jsonp() 在 JSONP 的支持下發送 JSON 響應
res.redirect() 重定向請求
res.render() 呈現視圖模板
res.send() 發送各類類型的響應
res.sendFile 以八位元流形式發送文件
res.sendStatus() 設置響應狀態碼並以響應主體形式發送其字符串表示
相關文章
相關標籤/搜索