如何在Meteor中使用NodeJS模块?
在一个NodeJS应用程序中,我已经完成了一些模块,现在我想在Meteor中使用它们,我应该怎么做? 例如,有一个文件“hello.js”,内容:如何在Meteor中使用NodeJS模块?,meteor,Meteor,在一个NodeJS应用程序中,我已经完成了一些模块,现在我想在Meteor中使用它们,我应该怎么做? 例如,有一个文件“hello.js”,内容: require('url');// In here,require other modules function sayHi(name){ console.log("Hi "+ name); } exports.sayHi = sayHi; 在《流星》中如何使用“打招呼” 当我这样做时: if (Meteor.isServer) {
require('url');// In here,require other modules
function sayHi(name){
console.log("Hi "+ name);
}
exports.sayHi = sayHi;
在《流星》中如何使用“打招呼”
当我这样做时:
if (Meteor.isServer) {
Meteor.startup(function () {
var require = __meteor_bootstrap__.require;
var index = require('./hello');
hello.syaHi('Ec');})}
错误是:
app/index.js:1
require();
^
ReferenceError: require is not defined
at app/index.js:1:1
at /home/huyinghuan/workspace/NodeJs/myMeteorJS/testrequire/.meteor/local/build/server/server.js:113:21
at Array.forEach (native)
at Function._.each._.forEach (/usr/lib/meteor/lib/node_modules/underscore/underscore.js:79:11)
at run (/home/huyinghuan/workspace/NodeJs/myMeteorJS/testrequire/.meteor/local/build/server/server.js:99:7)
我认为,您必须将模块安装/复制到projectdir/.meteor/local/build/server/node_模块中,该模块是指向/usr/local/meteor/lib/node_模块的链接。我用node.js模块跟踪器尝试了这一点,它成功了。每次更新meteor安装时,您都必须将文件复制到此目录中。我认为,您必须将模块安装/复制到projectdir/.meteor/local/build/server/node_模块中,该模块是指向/usr/local/meteor/lib/node_模块的链接。我用node.js模块跟踪器尝试了这一点,它成功了。每次更新meteor安装时,您都必须将文件复制到此目录中。此外,现在看来Npm.require是需要节点模块的正确方式。此外,现在看来Npm.require是需要节点模块的正确方式。更新,我必须将我的模块安装到.meteor/local/build/programs/server/node_模块以及使用Npm.require。更新,我必须将我的模块安装到.meteor/local/build/programs/server/node_模块以及使用Npm.require。以下是一个软件包,它使在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中使用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;
}
});
}
谢谢你的帮助。根据你的提示,我在《流星》中成功地使用了我的模子。谢谢你的帮助。根据你的提示,我在《流星》中成功地使用了我的模子。