中间件和应用程序在Expressjs中是什么意思? [英] What does middleware and app.use actually mean in Expressjs?

查看:96
本文介绍了中间件和应用程序在Expressjs中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几乎我看到的每个Express应用程序都有一个中间件的 app.use 语句,但是我没有找到一个清楚简明的解释,说明中间件实际上是什么, code> app.use 语句正在做。即使是快递文件本身也是有点模糊的。你能解释一下这些概念吗?

解决方案

中间件



我在一个新项目中分离中间件的概念的中途。



中间件允许您定义一个应该流经的动作堆栈。 Express服务器本身是一堆中间件。

  // express 
var app = express();
//中间件
var stack = middleware();

然后,您可以通过调用 .use

  // express 
app.use(express.static(..));
//中间件
stack.use(function(data,next){
next();
});

中间件堆栈中的一个层是一个函数,它需要n个参数(2表示express, req & res )和下一个函数。 >

中间件期望图层进行一些计算,扩充参数,然后调用 next



除非处理它,否则堆栈不会执行任何操作。每次在服务器上抓到传入的HTTP请求时,Express将处理堆栈。使用中间件,您可以手动处理堆栈。

  // express,你不需要做任何事情
// middleware
stack.handle(someData);

更完整的示例:

  var middleware = require(../ src / middleware.js); 

var stack = middleware(function(data,next){
data.foo = data.data * 2;
next();
},function数据,next){
setTimeout(function(){
data.async = true;
next();
},100)
},function ){
console.log(data);
});

stack.handle({
data:42
})

明确地说,您只需定义一个您希望为每个传入HTTP请求处理的一组操作。



在express(而不是连接)你有全局中间件和路由特定的中间件。这意味着您可以将中间件堆栈附加到每个传入的HTTP请求中,也可以将其附加到与特定路由交互的HTTP请求。



中间件:

  //中间件

var stack = middleware(function(req,res,next) {
users.getAll(function(err,users){
if(err)next(err);
req.users = users;
next();
$);
},function(req,res,next){
posts.getAll(function(err,posts){
if(err)next(err);
req.posts = posts;
next();
})
},function(req,res,next){
req.posts.forEach(function(post) {
post.user = req.users [post.userId];
});

res.render(博客/帖子,{
:req.posts
});
});

var app = express.createServer();

app.get(/ posts,function(req,res){
stack.handle(req,res);
});

// express

var app = express.createServer();

app.get(/ posts,[
function(req,res,next){
users.getAll(function(err,users){
if(err)next(err);
req.users = users;
next();
});
},function(req,res,next){
posts.getAll(function(err,posts){
if(err)next(err);
req.posts = posts;
next();
} )
},function(req,res,next){
req.posts.forEach(function(post){
post.user = req.users [post.userId];


res.render(blog / posts,{
posts:req.posts
});
}
],function(req,res){
stack.handle(req,res);
});


Almost every Express app I see has an app.use statement for middleware but I haven't found a clear, concise explanation of what middleware actually is and what the app.use statement is doing. Even the express docs themselves are a bit vague on this. Can you explain these concepts for me please?

解决方案

middleware

I'm halfway through separating the concept of middleware in a new project.

Middleware allows you to define a stack of actions that you should flow through. Express servers themselves are a stack of middlewares.

// express
var app = express();
// middleware
var stack = middleware();

Then you can add layers to the middleware stack by calling .use

// express
app.use(express.static(..));
// middleware
stack.use(function(data, next) {
  next();
});

A layer in the middleware stack is a function, which takes n parameters (2 for express, req & res) and a next function.

Middleware expects the layer to do some computation, augment the parameters and then call next.

A stack doesn't do anything unless you handle it. Express will handle the stack every time an incoming HTTP request is caught on the server. With middleware you handle the stack manually.

// express, you need to do nothing
// middleware
stack.handle(someData);

A more complete example :

var middleware = require("../src/middleware.js");

var stack = middleware(function(data, next) {
    data.foo = data.data*2;
    next();
}, function(data, next) {
    setTimeout(function() {
        data.async = true;
        next();
    }, 100)
}, function(data) {
    console.log(data);
});

stack.handle({
    "data": 42
})

In express terms you just define a stack of operations you want express to handle for every incoming HTTP request.

In terms of express (rather than connect) you have global middleware and route specific middleware. This means you can attach a middleware stack to every incoming HTTP requests or only attach it to HTTP requests that interact with a certain route.

Advanced examples of express & middleware :

// middleware 

var stack = middleware(function(req, res, next) {
    users.getAll(function(err, users) {
        if (err) next(err);
        req.users = users;
        next();  
    });
}, function(req, res, next) {
    posts.getAll(function(err, posts) {
        if (err) next(err);
        req.posts = posts;
        next();
    })
}, function(req, res, next) {
    req.posts.forEach(function(post) {
        post.user = req.users[post.userId];
    });

    res.render("blog/posts", {
        "posts": req.posts
    });
});

var app = express.createServer();

app.get("/posts", function(req, res) {
   stack.handle(req, res); 
});

// express

var app = express.createServer();

app.get("/posts", [
    function(req, res, next) {
        users.getAll(function(err, users) {
            if (err) next(err);
            req.users = users;
            next();  
        });
    }, function(req, res, next) {
        posts.getAll(function(err, posts) {
            if (err) next(err);
            req.posts = posts;
            next();
        })
    }, function(req, res, next) {
        req.posts.forEach(function(post) {
            post.user = req.users[post.userId];
        });

        res.render("blog/posts", {
            "posts": req.posts
        });
    }
], function(req, res) {
   stack.handle(req, res); 
});

这篇关于中间件和应用程序在Expressjs中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆