将节点模块导出为唯一实例

时间:2021-10-16 23:30:04

I'm using restify to write a basic REST API framework in node. To support this, I have several helper objects that I export and require.

我正在使用restify在节点中编写一个基本的REST API框架。为了支持这一点,我有几个我导出和需要的帮助对象。

I'm having trouble understanding how to require these objects in such a way that each incoming HTTP request uses a unique instance of the helper object. As it is, I require() the module inside the scope of the GET request, but I have a feeling that's not the best practice. For example:

我无法理解如何以这样的方式要求这些对象,即每个传入的HTTP请求都使用辅助对象的唯一实例。实际上,我需要()GET请求范围内的模块,但我觉得这不是最好的做法。例如:

helper.js

var helper = function(){
  this.foo = new Date().getTime();
  return this;
};
helper.prototype.test = function(){
  return new Date().getTime();
};
module.exports = new helper();

main.js

app.get("/", function(req, res){
  var helper = require("./helper");
  helper.test();
});

helper.test() works: each incoming request has it's own scoped instance of helper. If I require the helper file outside of the request scope, then data within the helper object is shared between requests, because of the way node caches the object of a required file.

helper.test()有效:每个传入的请求都有自己的帮助器作用域实例。如果我在请求范围之外需要帮助程序文件,那么辅助对象中的数据将在请求之间共享,因为节点缓存所需文件的对象的方式。

However, in this case, helper.foo is the same for each call, presumably because the require() call is receiving 1 instance via the "new" keyword.

但是,在这种情况下,helper.foo对于每个调用都是相同的,可能是因为require()调用通过“new”关键字接收1个实例。

I came across this issue when I had a helper function that used a random string to name a file, but because I had declared it like (below), all files were named the same.

当我有一个使用随机字符串命名文件的辅助函数时,我遇到了这个问题,但因为我已经声明它(如下),所有文件都被命名为相同。

helper.prototype.fileName = random(10) + ".json";

Does requiring files within the scope of a request have any negative consequences? I would imagine that when dealing with high frequency traffic, it would not be ideal to be requesting files for each call.

请求范围内的文件是否会产生任何负面后果?我想,在处理高频流量时,为每个呼叫请求文件并不理想。

What are the best practices for requiring / exporting modules while considering multiple incoming HTTP requests via express or restify? Should I just make sure the helpers I'm writing don't deal with these edge cases?

在通过快递或解决方案考虑多个传入HTTP请求时,要求/导出模块的最佳做法是什么?我应该确保我写的助手不处理这些边缘情况吗?

1 个解决方案

#1


2  

Okay, so first it would be good to go over what require does. Fred K. Schott describes this really well thenodeway.io. So essentially, the require function:

好的,所以首先回顾一下需要做什么就好了。 Fred K. Schott描述了这个非常好的thenodeway.io。基本上,require函数:

  1. Tries to load the file
  2. 尝试加载文件

  3. If it can, it returns module.exports
  4. 如果可以,则返回module.exports

  5. Caches the module instance away, incase you need to use it again.
  6. 缓存模块实例,因此需要再次使用它。

  7. The next time you require the same file, it returns the cached version.
  8. 下次需要相同文件时,它将返回缓存版本。

So after you require a file to begin with, there is no additional overhead to require it again.

因此,在您需要一个文件开头之后,再没有需要额外的开销。

Secondly, your question is a general javascript question. To achieve what you are wanting to achieve change your files to do this:

其次,你的问题是一般的JavaScript问题。要实现您想要实现的目标,请更改文件以执行此操作:

helper.js

var helper = function(){
  this.foo = new Date().getTime();
  this.fileName = random(10) + ".json";
  return this;
};
helper.prototype.test = function(){
  return new Date().getTime();
};
module.exports = helper;

main.js

var Helper = require("./helper");    
app.get("/", function(req, res){

  var helper = new Helper();
  helper.test()
});

That way helper is always unique for each request.

这样,帮助程序对于每个请求始终是唯一的。

So, try to use modules often when you are segmenting your code. They only add initial overhead, and do not inherently affect performance for heavy traffic.

因此,在分割代码时,请经常尝试使用模块。它们只会增加初始开销,并且不会因为流量过大而影响性能。

#1


2  

Okay, so first it would be good to go over what require does. Fred K. Schott describes this really well thenodeway.io. So essentially, the require function:

好的,所以首先回顾一下需要做什么就好了。 Fred K. Schott描述了这个非常好的thenodeway.io。基本上,require函数:

  1. Tries to load the file
  2. 尝试加载文件

  3. If it can, it returns module.exports
  4. 如果可以,则返回module.exports

  5. Caches the module instance away, incase you need to use it again.
  6. 缓存模块实例,因此需要再次使用它。

  7. The next time you require the same file, it returns the cached version.
  8. 下次需要相同文件时,它将返回缓存版本。

So after you require a file to begin with, there is no additional overhead to require it again.

因此,在您需要一个文件开头之后,再没有需要额外的开销。

Secondly, your question is a general javascript question. To achieve what you are wanting to achieve change your files to do this:

其次,你的问题是一般的JavaScript问题。要实现您想要实现的目标,请更改文件以执行此操作:

helper.js

var helper = function(){
  this.foo = new Date().getTime();
  this.fileName = random(10) + ".json";
  return this;
};
helper.prototype.test = function(){
  return new Date().getTime();
};
module.exports = helper;

main.js

var Helper = require("./helper");    
app.get("/", function(req, res){

  var helper = new Helper();
  helper.test()
});

That way helper is always unique for each request.

这样,帮助程序对于每个请求始终是唯一的。

So, try to use modules often when you are segmenting your code. They only add initial overhead, and do not inherently affect performance for heavy traffic.

因此,在分割代码时,请经常尝试使用模块。它们只会增加初始开销,并且不会因为流量过大而影响性能。