Node.JS 异步加载模块

标签 node.js asynchronous module require

我希望能够加载已更改的模块。我当然要unload the module first .由于这是一个网络服务器设置,我想问是否有办法以异步方式加载模块,以避免在读取更新文件期间卡住网络服务器。

不久前 Node.JS 删除了 require.async 函数。那么,在最新版本的 Node.JS 上,推荐的替代方案是什么?

  • 要不要先读整个文件,然后用Module库解析文件内容。 (与采用文件名在内部读取和解析的默认功能相反)如何?
  • 我应该将这项工作外包给一些开源库吗?哪一个?
  • 我应该编写自己的模块处理程序 - 我自己的 requireAsync 实现吗? (我知道怎么做。)

注意:除了异步加载模块,我不想做任何事情,所以请不要建议我用新的网络服务器路由框架替换我的设置。

最佳答案

我发布了这个答案,但欢迎您发布改进。

the Node.JS source code .

Module.prototype.require = function(path) { return Module._load(path, this); };

_load

的精简版
Module._load = function(request, parent, isMain) {
  var filename = Module._resolveFilename(request, parent);
  if (Module._cache[filename]) return Module._cache[filename].exports;
  if (NativeModule.exists(filename)) {
    if (filename == 'repl') { // special case, needs the real require.
      var replModule = new Module('repl');
      replModule._compile(NativeModule.getSource('repl'), 'repl.js');
      NativeModule._cache.repl = replModule;
      return replModule.exports;
    }
    return NativeModule.require(filename);
  }
  var module = new Module(filename, parent);
  if (isMain) process.mainModule = module, module.id = '.';
  Module._cache[filename] = module;
  var hadException = true;
  try {
    module.load(filename);
    hadException = false;
  } finally {
    if (hadException) delete Module._cache[filename];
  }
  return module.exports;
};

我的 require.async.js 版本类似于

var NativeModule = require('native_module');
var fs = require('fs');
if(!require.async) require.async = function (path, callback) { module.exports(path, this, callback); } // Comment out if you dislike using globals
module.exports = function(request, parent, callback) {
  var filename = Module.resolve(request, parent); // This is a Sync function. TODO, change it to an async function with a callback.
  if (Module.cache[filename]) callback(Module.cache[filename].exports);
  else if (NativeModule.exists(filename)) callback(new Error('What are you thinking?'))
  else fs.readFile(filename, 'utf8', function(err, file) {
    if (Module.cache[filename]) callback(null, Module.cache[filename].exports); // For the case when there are two calls to require.async at a time.
    else if(err) callback(err)
    else {
      var module = new Module(filename, parent);
      try {
        module._compile(file);
        Module.cache[filename] = module;
      } catch(ex) {
        callback(err)
      }
      if(Module.cache[filename]) callback(null, module.exports)
    }
}

注意事项

  • 代码中有一个 TODO 就是要对 stat 的多次调用进行异步调用。文件的实际读取是常规异步,所以很好。
  • 如果您正在异步加载一个模块,并且您正在加载的该模块正在同步加载另一个模块,那么您的代码还没有完全异步 - 有吗。
  • 它使用一种私有(private)方法 - _compile

关于Node.JS 异步加载模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13917420/

相关文章:

node.js - KeystoneJS watch :lint is not in your gulpfile

javascript - socket.io.js 未加载

javascript - winston - 如何记录导致 Node 进程崩溃的错误?

android - 从 Android Studio (1.0.2) 中的其他项目导入模块

ruby-on-rails - Ruby:将方法添加到现有模块

wpf - 运行目录的所有模块( Prism )

node.js - 在node.js中使用bitly策略实现 Passport 认证

java - OkHttp3 SocketTimeoutException (Android app) -- 如何设置异步请求的读取超时时间?

javascript - Simon游戏javascript,如何使用animation()和delay() JQUERY同步按钮的灯光而不互相踩踏?

javascript - native react : render function not being called after state change