节点。js和Express -扩展请求对象是一个好主意吗?

时间:2021-01-10 16:54:43

i am starting to learn Node.js and trying to understand the architecture of it combined with the micro-framework Express.

我开始学习Node。并尝试结合微框架Express来理解它的架构。

I see that Express uses Connect as a middleware. Connect augments the request and response objects with all kinds of stuff in a chain of functions, and it provides an API so you can add custom middleware. I guess this augmenting is a way to keep things simple and flexible in the handlers/controllers, instead of having a variable number of parameters and parameter types. Here is an example of a simple GET handler:

我看到Express将Connect用作中间件。将请求和响应对象与函数链中的各种东西连接起来,并提供一个API,以便您可以添加自定义中间件。我猜这种扩充是一种在处理程序/控制器中保持简单和灵活的方法,而不是拥有可变数量的参数和参数类型。这里有一个简单的GET处理程序的例子:

app.get('/', function (req, res) {
    res.render('index', { title: 'Hey', message: 'Hello there!'});
})

In tutorials from Node.js experts i have seen stuff like augmenting the request object with a MongoDB collection. In a blog from Azat Mardan i have seen this code :

教程从节点。js专家我看到过一些东西,比如用MongoDB来增加请求对象。在Azat Mardan的博客上,我看到了这样的代码:

var db = mongoskin.db('mongodb://@localhost:27017/test', {safe:true})

app.param('collectionName', function(req, res, next, collectionName){
   req.collection = db.collection(collectionName)
   return next()
})

The approach above is using the 'collectionName' parameter in the route name as a conditional to control the augmentation of the request. However, i have seen uglier code where the database middleware is attached on EVERY request that goes through Node.js without this conditional approach.

上面的方法是使用路径名中的“集合名称”参数作为条件来控制请求的扩展。但是,我已经看到了更难看的代码,其中的数据库中间件附加在通过节点的每个请求上。没有这种条件方法的js。

Looking at standard software principles like single responsibility principle, separation of concerns and testability why is it a good idea to extend the request with a MongoDB collection object and dozens of other objects? isn't the request and response object bloated with functionality this way and has unpredictable state and behavior? Where does this pattern come from and what are the pros and cons and alternatives?

看看标准的软件原则,比如单一职责原则、关注点分离和可测试性,为什么用MongoDB集合对象和其他数十个对象扩展请求是个好主意?请求和响应对象不是以这种方式膨胀并具有不可预测的状态和行为吗?这种模式从何而来?它的正反两面是什么?

1 个解决方案

#1


3  

This is fine. IMHO the very purpose of the request object is as a container to pass things down the stack for other handlers to use. It is far cleaner than looking for some agreed-upon-named global holder.

这是很好。IMHO请求对象的真正目的是作为一个容器向堆栈传递东西,供其他处理程序使用。这要比寻找某个公认的全球持有者干净得多。

You could argue that it should be mostly empty, and then have the "official" request and response functionality on some property of the request/response objects, so it is cleaner, but I think the benefits are minimal.

您可能会认为它应该大部分为空,然后在请求/响应对象的某些属性上具有“官方”请求和响应功能,因此它更简洁,但我认为好处是最小的。

Matter of fact, just about every middleware I have seen, including looking at the express source code and ones I have authored, uses request for exactly this sort of "container to pass properties and functionalities down the handler stack".

事实上,我所看到的所有中间件,包括查看express源代码和我编写的那些,都使用了这种“容器将属性和功能向下传递到处理程序堆栈”的请求。

#1


3  

This is fine. IMHO the very purpose of the request object is as a container to pass things down the stack for other handlers to use. It is far cleaner than looking for some agreed-upon-named global holder.

这是很好。IMHO请求对象的真正目的是作为一个容器向堆栈传递东西,供其他处理程序使用。这要比寻找某个公认的全球持有者干净得多。

You could argue that it should be mostly empty, and then have the "official" request and response functionality on some property of the request/response objects, so it is cleaner, but I think the benefits are minimal.

您可能会认为它应该大部分为空,然后在请求/响应对象的某些属性上具有“官方”请求和响应功能,因此它更简洁,但我认为好处是最小的。

Matter of fact, just about every middleware I have seen, including looking at the express source code and ones I have authored, uses request for exactly this sort of "container to pass properties and functionalities down the handler stack".

事实上,我所看到的所有中间件,包括查看express源代码和我编写的那些,都使用了这种“容器将属性和功能向下传递到处理程序堆栈”的请求。