Node.js的加载模块异步 [英] Node.JS load module async

查看:143
本文介绍了Node.js的加载模块异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想必须加载已更改一个模块的能力。我不得不当然卸载模块首先。由于这是一个网络服务器的设置,我问,如果有加载模块中的异步的方式,以避免冻结Web服务器的更新文件的读取时间的方式。

I would like to have the ability to load a module that has been changed. I would have to of course unload the module first. Since this is a webserver setup, I am asking if there is a way to load the module in an async fashion, to avoid freezing the webserver for the duration of the read of the updated file.

一段时间回来的Node.js删除 require.async 功能。因此,对Node.js的最新版本,这将是推荐的选择吗?

Awhile back Node.JS removed the require.async function. So, on the latest version of Node.JS, what would be the recommended alternative?


  • 我应该先读入整个文件,然后使用模块库来解析该文件的内容。 (相对于取一个文件名的默认功能来读取和内部解析)如何?

  • 我应该外包这一工作,一些开源库?哪一个?

  • 我应该写我自己的模块处理程序 - 我自己实施 requireAsync 的? (我知道怎么办。)

  • Should I read the entire file first, and then use the Module library to parse the file contents. (as opposed to the default functionality of taking a file name to read and parse internally) How?
  • Should I outsource this job to some open-source library? Which one?
  • Should I write my own module handler - my own implementation of requireAsync? (I know how.)

<子>注:我不想做任何事情超过加载模块异步,所以请不要推荐我一个新的Web服务器的路由框架来代替我的设置

推荐答案

我贴这个答案,但我们欢迎您发布的改善。

I posted this answer, but you are welcome to post an improvement.

请参阅 Node.js的源$ C ​​$ C

Module.prototype.require =功能(路径){返回Module._load(路径,这一点); };

删节版 _load

Abridged version of _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 的将是这样

My version of require.async.js will be something like

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

注意事项


  • 有一个在$ C $一个系统一台TODO这是做出多个呼叫与stat 是异步。该文件的实际读数是普通异步,所以这是很好的。

  • 如果您正在异步加载一个模块,您正在加载的模块加载同步另一个模块,那么你还没有完全消失异步与code - 有你

  • 它使用一个私有方法 - _compile

  • There is one TODO in the code which is to make the multiple calls to stat to be async. The actual reading of the file is regular async, so that is good.
  • If you are async loading a module, and that module you are loading is sync loading another module, then you have not fully gone async with your code - have you.
  • It uses one private method - _compile.

这篇关于Node.js的加载模块异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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