完整的 Meteor NPM 集成

在Meteor中,你只能使用包内的模块。你不能直接将模块与流星应用一起使用。此软件包解决了该问题

源码下载地址

点击这里下载源码

安装

meteor add meteorhacks:npm

然后启动您的应用并按照说明进行操作。

定义软件包

初始化 npm 支持后,你的应用内将有一个称为文件名的文件名。在该文件中定义包,如下所示。

{
  "redis": "0.8.2",
  "github": "0.1.8"
}

您必须为 npm 模块定义一个绝对版本号

如果需要从特定提交安装 npm 模块,请使用以下语法:

{
  "googleapis": "https://github.com/bradvogel/google-api-nodejs-client/archive/d945dabf416d58177b0c14da64e0d6038f0cc47b.tar.gz"
}

以上内容可以使用 github 版本生成。你要用的是版本,而不是.<commit hash>.tar.gzarchive/<version number>.tar.gz

使用软件包

你可以使用 method 访问服务器端的 npm 模块,并随心所欲地使用它。 大多数 npm 模块都提供带有回调或承诺的异步 API。所以,你不能直接在Meteor上使用它们。正因为如此,这个软件包附带了一组方便的异步实用程序,让你的生活更轻松。

在 Meteor 方法中使用 npm 模块的示例

if (Meteor.isClient) {
  getGists = function getGists(user, callback) {
    Meteor.call('getGists', user, callback);
  }
}

if (Meteor.isServer) {
  Meteor.methods({
    'getGists': function getGists(user) {
      var GithubApi = Meteor.npmRequire('github');
      var github = new GithubApi({
          version: "3.0.0"
      });

      var gists = Async.runSync(function(done) {
        github.gists.getFromUser({user: 'arunoda'}, function(err, data) {
          done(null, data);
        });
      });

      return gists.result;
    }
  });
}

应用程序接口

仅在服务器端可用

Meteor.npmRequire(npmModule名称)
此方法加载您在文件中指定的 NPM 模块。

var Github = Meteor.npmRequire('github');

Meteor.require(npmModule名称)
同上。但已弃用。

异步实用程序

仅在服务器端可用 Async Utitlies 可以通过
meteorhacks:async 作为单独的软件包提供

Meteor API 是同步执行的。大多数 NodeJS 模块都是异步工作的。 因此,我们需要一种方法来弥补差距。Async Utilities 来拯救你。

Async.runSync(函数)

Async.runSync()暂停执行,直到调用 callback,如下所示。done()

var response = Async.runSync(function(done) {
  setTimeout(function() { 
    done(null, 1001);
  }, 100);
});

console.log(response.result); // 1001

done()callback 需要 2 个参数。 和对象。您可以将它们作为 的返回值获取,如上例中的响应所示。errorresultAsync.runSync()
返回值是一个对象,它有 2 个字段。 和。error result

Meteor.sync(函数)

相同,但已弃用。Async.runSync

Async.wrap(函数)

包装一个异步函数,并允许它在 Meteor 中运行,没有回调。


//declare a simple async function
function delayedMessage(delay, message, callback) {
  setTimeout(function() {
    callback(null, message);
  }, delay);
}

//wrapping
var wrappedDelayedMessage = Async.wrap(delayedMessge);

//usage
Meteor.methods({
  'delayedEcho': function(message) {
    var response = wrappedDelayedMessage(500, message);
    return response;
  }
});

如果回调有结果,它将从包装的函数返回。如果出现错误,则会抛出。

Async.wrap(function)与 非常相似。
Meteor._wrapAsync

Async.wrap(对象,函数名称)

与 非常相似,, 但此 API 可用于包装对象的实例方法。Async.wrap(function)

var github = new GithubApi({
    version: "3.0.0"
});

//wrapping github.user.getFrom
var wrappedGetFrom = Async.wrap(github.user, 'getFrom');

Async.wrap(对象,函数名称列表)

与 非常相似,, 但此 API 可用于包装对象的多个实例方法。Async.wrap(object, functionName)

var github = new GithubApi({
    version: "3.0.0"
});

//wrapping github.user.getFrom and github.user.getEmails
var wrappedGithubUser = Async.wrap(github.user, ['getFrom', 'getEmails']);

//usage
var profile = wrappedGithubUser.getFrom('arunoda');
var emails = wrappedGithubUser.getEmails();
  • 20
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mldxxxxll5

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值