bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

express.js中間件是什么-創新互聯

這篇文章主要介紹express.js中間件是什么,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

創新互聯公司專注于企業網絡營銷推廣、網站重做改版、臨滄網站定制設計、自適應品牌網站建設、成都h5網站建設商城建設、集團公司官網建設、成都外貿網站建設公司、高端網站制作、響應式網頁設計等建站業務,價格優惠性價比高,為臨滄等各大城市提供網站開發制作服務。

express的新開發人員往往對路由處理程序和中間件之間的區別感到困惑。因此他們也對app.use(),app.all(),app.get(),app.post(),app.delete()和app.put()方法的區別感到困惑。

在本文中,我將解釋中間件和路由處理程序之間的區別。以及如何正確使用app.use(),app.all(),app.get(),app.post(),app.delete()和app.put()方法。

路由處理

app.use(),app.all(),app.get(),app.post(),app.delete()和app.put()全部是用來定義路由的。這些方法都用于定義路由。路由用于處理HTTP請求。路由是路徑和回調的組合,在請求的路徑匹配時執行。回調被稱為路由處理程序。

它們之間的區別是處理不同類型的HTTP請求。例如: app.get()方法僅僅處理get請求,而app.all()處理GET、POST等請求。

下面是一個例子,如何定義一個路由:

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

app.get("/", function(req, res, next){
    res.send("Hello World!!!!");
});

app.listen(8080);

每個路由處理程序都獲得對當前正在提供的HTTP請求的請求和響應對象的引用。

可以為單個HTTP請求執行多個路由處理程序。這是一個例子:
var app = require("express")();

app.get("/", function(req, res, next){
    res.write("Hello");
    next();
});

app.get("/", function(req, res, next){
    res.write(" World !!!");
    res.end();
});

app.listen(8080);

這里第一個句柄寫入一些響應,然后調用next()next()方法用于調用與路徑路徑匹配的下一個路由處理程序。

路由處理程序必須結束請求或調用下一個路由處理程序。

我們還可以將多個路由處理程序傳遞給app.all()app.get()app.post()app.delete()app.put()方法。

這是一個證明這一點的例子:
var app = require("express")();

app.get("/", function(req, res, next){
    res.write("Hello");
    next();
}, function(req, res, next){
    res.write(" World !!!");
    res.end();
});

app.listen(8080);
中間件

中間件是一個位于實際請求處理程序之上的回調。它采用與路由處理程序相同的參數。

要了解中間件,我們來看一個帶有dashboardprofile頁面的示例站點。要訪問這些頁面,用戶必須登錄。還會記錄對這些頁面的請求。

以下是這些頁面的路由處理程序的代碼:
var app = require("express")();

function checkLogin(){
    return false;
}

function logRequest(){
    console.log("New request");
}

app.get("/dashboard", function(req, res, next){

    logRequest();

    if(checkLogin()){
        res.send("This is the dashboard page");
    }
    else{
        res.send("You are not logged in!!!");
    }
});

app.get("/profile", function(req, res, next){

    logRequest();

    if(checkLogin()){
        res.send("This is the dashboard page");
    }
    else{
        res.send("You are not logged in!!!");
    }
});

app.listen(8080);

這里的問題是有很多重復的代碼,即我們不得不多次使用logRequest()checkLogin()函數。這也使得更新代碼變得困難。因此,為了解決這個問題,我們可以為這兩條路徑編寫一條通用路徑。

這是重寫的代碼:
var app = require("express")();

function checkLogin(){
    return false;
}

function logRequest(){
    console.log("New request");
}

app.get("/*", function(req, res, next){
    logRequest();
    next();
})

app.get("/*", function(req, res, next){
    if(checkLogin()){
        next();
    }
    else{
        res("You are not logged in!!!");
    }
})

app.get("/dashboard", function(req, res, next){
    res.send("This is the dashboard page");
});

app.get("/profile", function(req, res, next){
    res.send("This is the dashboard page");
});

app.listen(8080);

這里的代碼看起來更清晰,更易于維護和更新。這里將前兩個定義的路由處理程序稱為中間件,因為它們不處理請求,而是負責預處理請求。

Express為我們提供了app.use()方法,該方法專門用于定義中間件。 app.use()方法可能看起來與app.all()類似,但它們之間存在很多差異,這使得app.use()非常適合于聲明中間件。讓我們看看app.use()方法是如何工作的:

app.use() 和 app.all() 的不同:

CALLBACK

app.use()只需要一個回調,而app.all()可以進行多次回調。

PATH

app.use()只查看url是否以指定路徑開頭,app.all()匹配完整路徑。

這里有一個例子來說明:
app.use( "/product" , mymiddleware);
// will match /product
// will match /product/cool
// will match /product/foo

app.all( "/product" , handler);
// will match /product
// won't match /product/cool   <-- important
// won't match /product/foo    <-- important

app.all( "/product/*" , handler);
// won't match /product        <-- Important
// will match /product/cool
// will match /product/foo

NEXT()

中間件內的next()調用下一個中間件或路由處理程序,具體取決于接下來聲明的那個。但是路由處理程序中的next()僅調用下一個路由處理程序。如果接下來有中間件,則跳過它。因此,必須在所有路由處理程序之前聲明中間件。

這里有一個例子來說明:

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

app.use(function frontControllerMiddlewareExecuted(req, res, next){
  console.log('(1) this frontControllerMiddlewareExecuted is executed');
  next();
});

app.all('*', function(req, res, next){
  console.log('(2) route middleware for all method and path pattern "*", executed first and can do stuff before going next');
  next();
});

app.all('/hello', function(req, res, next){
  console.log('(3) route middleware for all method and path pattern "/hello", executed second and can do stuff before going next');
  next();
});

app.use(function frontControllerMiddlewareNotExecuted(req, res, next){
  console.log('(4) this frontControllerMiddlewareNotExecuted is not executed');
  next();
});

app.get('/hello', function(req, res){
  console.log('(5) route middleware for method GET and path patter "/hello", executed last and I do my stuff sending response');
  res.send('Hello World');
});

app.listen(80);

現在我們看到了app.use()方法的唯一性以及它用于聲明中間件的原因。

讓我們重寫我們的示例站點代碼:
var app = require("express")();

function checkLogin(){
    return false;
}

function logRequest(){
    console.log("New request");
}

app.use(function(req, res, next){
    logRequest();
    next();
})

app.use(function(req, res, next){

    if(checkLogin()){
        next();
    }
    else{
        res.send("You are not logged in!!!");
    }
})

app.get("/dashboard", function(req, res, next){
    res.send("This is the dashboard page");
});

app.get("/profile", function(req, res, next){
    res.send("This is the dashboard page");
});

app.listen(8080);

以上是“express.js中間件是什么”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注創新互聯網站制作公司行業資訊頻道!

文章題目:express.js中間件是什么-創新互聯
文章源于:http://vcdvsql.cn/article32/ccspsc.html

成都網站建設公司_創新互聯,為您提供搜索引擎優化網站排名商城網站用戶體驗微信小程序Google

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

綿陽服務器托管