在 Expressjs 中中间件和 app.use 实际上是什么意思? [英] What does middleware and app.use actually mean in Expressjs?

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

问题描述

我看到的几乎每个 Express 应用程序都有一个关于中间件的 app.use 语句,但我还没有找到关于中间件究竟是什么以及 app.use<的清晰、简洁的解释/code> 语句正在执行.甚至快递文档本身对此也有点含糊.你能帮我解释一下这些概念吗?

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?

推荐答案

中间件

我正在一个新项目中分离中间件的概念.

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

中间件允许你定义一系列你应该通过的动作.Express 服务器本身就是一堆中间件.

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

然后你可以通过调用.use

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

中间件栈中的一层是一个函数,它接受n个参数(2个为express,req & res)和一个next 功能.

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

中间件期望层做一些计算,增加参数,然后调用next.

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

除非你处理它,否则堆栈不会做任何事情.每次在服务器上捕获传入的 HTTP 请求时,Express 都会处理堆栈.使用中间件,您可以手动处理堆栈.

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

一个更完整的例子:

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

在 express 术语中,您只需定义要 express 为每个传入 HTTP 请求处理的操作堆栈.

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

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

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.

express & 高级示例中间件:

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 中中间件和 app.use 实际上是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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