1. Install the fibers node package on the server.
npm -g install fibers
2. Define the server methods in server.js.
server/server.js
Meteor.startup(function () { // Load future from fibers var Future = Npm.require("fibers/future"); // Load exec var exec = Npm.require("child_process").exec; // Server methods Meteor.methods({ runCode: function () { // This method call won't return immediately, it will wait for the // asynchronous code to finish, so we call unblock to allow this client // to queue other method calls (see Meteor docs) this.unblock(); var future=new Future(); var command="pwd"; exec(command,function(error,stdout,stderr){ if(error){ console.log(error); throw new Meteor.Error(500,command+" failed"); } future.return(stdout.toString()); }); return future.wait(); } }); });
3. Call the server method on client side.
client/js/main.js
Meteor.call('runCode', function (err, response) { console.log(response); });
4. Here is another example if the server method have input parameters.
server/server.js
Meteor.methods({ runCode: function (x, y) { ...
client/js/main.js
Meteor.call('runCode', "value x", "value y", function (err, response) { console.log(response); });
Done =)
Reference:
npm -g install fibres
npm ERR! 404 Not Found
npm ERR! 404
npm ERR! 404 ‘fibres’ is not in the npm registry.
npm ERR! 404 You should bug the author to publish it
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, or http url, or git url.
Is the package by chance “fibers”?
LikeLike
You are right, i have updated the post. Sorry for the mistake and thanks for pointing it out. =)
LikeLike
I have been having some issues…
I added:
// load Future
Future = Npm.require(‘fibers/future’);
//existing code
var future=new Future();
After adding that and running, I get the following in the chromium console:
Undefined main.js:10
But in that file, we only go up to line 3.
LikeLike
have u tried to click in the chrome console on the main.js:10 and check which line it is referring to?
LikeLike
Thanks for this! 🙂
LikeLike
Thanks for your comment. =)
LikeLike
instead of this particular ipaddress in fping “10.58.116.5” , i want to pass the variable ipToPing to it. console.log(ipToPing) is working fine. can we do that? Thanks in advance
LikeLike