百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>JS教程> express.js中心件的介绍(附示例)
分享文章到:

express.js中心件的介绍(附示例)

发布时间:09/01 来源:未知 浏览: 关键词:
本篇文章给大家带来的内容是关于express.js中心件的介绍(附示例),有必然的参照 价值,有需要的伴侣可以参照 一下,但愿对你有所帮忙。

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

本篇文章到这里就已经全部完毕了,更多其他出色内容可以关注PHP中文网的JavaScript视频教程栏目!

以上就是express.js中心件的介绍(附示例)的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有150人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板