我看到的几乎每个Express应用程序都有一个app.use语句用于中间件,但我还没有找到一个清晰、简洁的解释来说明中间件到底是什么以及app.use语句在做什么。甚至express文档本身在这一点上也有点模糊。你能给我解释一下这些概念吗?
当前回答
Middleware is a subset of chained functions called by the Express js routing layer before the user-defined handler is invoked. Middleware functions have full access to the request and response objects and can modify either of them. The middleware chain is always called in the exact order in which it has been defined, so it is vital for you to know exactly what a specific piece of middleware is doing. Once a middleware function finishes, it calls the next function in the chain by invoking its next argument as function. After the complete chain gets executed,the user request handler is called.
其他回答
在非常基本的术语中,如果我想这样解释它,我是从traversymedia youtube频道express速成课程中学到的。 中间件是一个函数,它在你像这样调用路由之后执行。
var logger = function(req, res, next){
console.log('logging...');
next();
}
app.use(logger);
这个记录器函数在你每次刷新页面时执行这意味着你可以在页面呈现后写任何你需要做的事情任何api调用,重置基本上任何事情。把这个中间件放在你的路由功能之前,中间件的顺序非常重要,否则它就无法工作
中间件是在输入/源产生输出后在中间执行的函数,该输出可以是最终输出,也可以被下一个中间件使用,直到循环完成。
它就像一个经过流水线的产品,在移动过程中不断修改,直到完成、评估或被拒绝。
中间件期望处理某些值(即参数值),并且基于某些逻辑,中间件将调用或不调用下一个中间件,或将响应发送回客户端。
如果您仍然不能理解中间件的概念,那么它在某种程度上类似于Decorator或命令链模式。
中间件
在一个新项目中,我已经把中间件的概念分离了一半。
中间件允许您定义应该流经的操作堆栈。Express服务器本身就是一堆中间件。
// 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函数。
中间件期望该层做一些计算,增加参数,然后调用next。
除非你处理它,否则堆栈不会做任何事情。每当在服务器上捕获到传入的HTTP请求时,Express将处理堆栈。对于中间件,您可以手动处理堆栈。
// 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
})
用简单的术语来说,您只需定义一个操作堆栈,以便为每个传入的HTTP请求表示处理。
在express(而不是connect)方面,有全局中间件和特定于路由的中间件。这意味着您可以将中间件堆栈附加到每个传入的HTTP请求上,或者仅将其附加到与特定路由交互的HTTP请求上。
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);
});
Middleware is a subset of chained functions called by the Express js routing layer before the user-defined handler is invoked. Middleware functions have full access to the request and response objects and can modify either of them. The middleware chain is always called in the exact order in which it has been defined, so it is vital for you to know exactly what a specific piece of middleware is doing. Once a middleware function finishes, it calls the next function in the chain by invoking its next argument as function. After the complete chain gets executed,the user request handler is called.
经过简化之后,web服务器可以被看作是一个接收请求并输出响应的函数。因此,如果您将web服务器视为一个功能,您可以将其组织成几个部分,并将它们分离为更小的功能,以便它们的组合将是原始功能。
中间件是可以与其他函数组合的较小函数,其明显的好处是可以重用它们。
推荐文章
- ReferenceError: description没有定义NodeJs
- 将一个二进制的NodeJS Buffer转换为JavaScript的ArrayBuffer
- AngularJS只适用于单页应用程序吗?
- 如何在vue-cli项目中更改端口号
- 同步和异步编程(在node.js中)的区别是什么?
- 如何编辑通过npm安装的节点模块?
- “node_modules”文件夹应该包含在git存储库中吗
- 使用package.json在全局和本地安装依赖项
- this.libOptions.parse不是一个函数
- 对嵌套文件夹运行npm install的最好方法是什么?
- 节点Multer异常字段
- 很好的初学者教程socket.io?
- CALL_AND_RETRY_LAST分配失败-进程内存不足
- 在Ubuntu上安装Node.js
- 使用express.js代理