内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

express.js中間件是什么-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)公司專(zhuān)注于企業(yè)網(wǎng)絡(luò)營(yíng)銷(xiāo)推廣、網(wǎng)站重做改版、臨滄網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)、商城建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)公司、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性?xún)r(jià)比高,為臨滄等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。

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

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

路由處理

app.use(),app.all(),app.get(),app.post(),app.delete()和app.put()全部是用來(lái)定義路由的。這些方法都用于定義路由。路由用于處理HTTP請(qǐng)求。路由是路徑和回調(diào)的組合,在請(qǐng)求的路徑匹配時(shí)執(zhí)行?;卣{(diào)被稱(chēng)為路由處理程序。

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

下面是一個(gè)例子,如何定義一個(gè)路由:

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

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

app.listen(8080);

每個(gè)路由處理程序都獲得對(duì)當(dāng)前正在提供的HTTP請(qǐng)求的請(qǐng)求和響應(yīng)對(duì)象的引用。

可以為單個(gè)HTTP請(qǐng)求執(zhí)行多個(gè)路由處理程序。這是一個(gè)例子:
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);

這里第一個(gè)句柄寫(xiě)入一些響應(yīng),然后調(diào)用next()。next()方法用于調(diào)用與路徑路徑匹配的下一個(gè)路由處理程序。

路由處理程序必須結(jié)束請(qǐng)求或調(diào)用下一個(gè)路由處理程序。

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

這是一個(gè)證明這一點(diǎn)的例子:
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);
中間件

中間件是一個(gè)位于實(shí)際請(qǐng)求處理程序之上的回調(diào)。它采用與路由處理程序相同的參數(shù)。

要了解中間件,我們來(lái)看一個(gè)帶有dashboardprofile頁(yè)面的示例站點(diǎn)。要訪問(wèn)這些頁(yè)面,用戶(hù)必須登錄。還會(huì)記錄對(duì)這些頁(yè)面的請(qǐng)求。

以下是這些頁(yè)面的路由處理程序的代碼:
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);

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

這是重寫(xiě)的代碼:
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);

這里的代碼看起來(lái)更清晰,更易于維護(hù)和更新。這里將前兩個(gè)定義的路由處理程序稱(chēng)為中間件,因?yàn)樗鼈儾惶幚碚?qǐng)求,而是負(fù)責(zé)預(yù)處理請(qǐng)求。

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

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

CALLBACK

app.use()只需要一個(gè)回調(diào),而app.all()可以進(jìn)行多次回調(diào)。

PATH

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

這里有一個(gè)例子來(lái)說(shuō)明:
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()

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

這里有一個(gè)例子來(lái)說(shuō)明:

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);

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

讓我們重寫(xiě)我們的示例站點(diǎn)代碼:
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中間件是什么”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站制作公司行業(yè)資訊頻道!

文章題目:express.js中間件是什么-創(chuàng)新互聯(lián)
文章源于:http://www.rwnh.cn/article32/ccspsc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站排名、商城網(wǎng)站用戶(hù)體驗(yàn)、微信小程序、Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

綿陽(yáng)服務(wù)器托管
兴化市| 石河子市| 灵寿县| 南皮县| 四会市| 海丰县| 托克逊县| 江孜县| 宁都县| 井陉县| 岢岚县| 广安市| 商河县| 绿春县| 阿拉善盟| 德州市| 喀什市| 汨罗市| 镇安县| 衡水市| 霍城县| 梅州市| 屏南县| 新干县| 康乐县| 铜山县| 丰镇市| 太仆寺旗| 肥乡县| 潮州市| 迭部县| 都安| 碌曲县| 南华县| 辽阳县| 沅江市| 常山县| 伽师县| 灵璧县| 东山县| 济宁市|