Sometimes I find myself wanting to execute some privileged code on the server while the app is running. For example, I may want to quickly delete a document in a collection (when the client-side is blocked from doing so). Or, I may want to quickly try out server-side functions like Email.send
and Accounts.createUser
.
So what are some of the ways of achieving this? I'm concerned with both cases of how an meteor app can be run:
- running using the
meteor
mand - running as the bundled node app
Ultimately, I'd also like to setup cron jobs that can execute some code in the Meteor context. Is this directly achievable or doable through workarounds?
Thanks for the help!
Sometimes I find myself wanting to execute some privileged code on the server while the app is running. For example, I may want to quickly delete a document in a collection (when the client-side is blocked from doing so). Or, I may want to quickly try out server-side functions like Email.send
and Accounts.createUser
.
So what are some of the ways of achieving this? I'm concerned with both cases of how an meteor app can be run:
- running using the
meteor
mand - running as the bundled node app
Ultimately, I'd also like to setup cron jobs that can execute some code in the Meteor context. Is this directly achievable or doable through workarounds?
Thanks for the help!
Share Improve this question asked Dec 20, 2012 at 8:56 DaveDave 12.5k10 gold badges48 silver badges52 bronze badges4 Answers
Reset to default 7Couldn't you just write server-side methods that only work for your user? Then expose those with Meteor.methods
and run them in the client console. That's what I do when I want to test eg. Email.send
. You could also go a step further and write a rudimentary admin UI.
For instance, on the server:
Meteor.methods({
test_sendEmail: function(options) {
if (this.userId != adminUserId) return; // don't execute unless admin
Email.send(options);
}
});
On the client:
Meteor.call("test_sendEmail", {to: "[email protected]", subject: "Foo", text: "Bar"});
To interact with the database while your app is running, you can do meteor mongo
in the root directory of your app. This will start a mongo shell, but you can't test server-side functions in it. As of now, I don't think there's a way to actually run a server-side console in a (not bundled) meteor app, but maybe you can meteor bundle
your app, run it with node, and then find a way to start a server-side console.
You could invoke meteor server side code by simulating a browser using curl. If you launch meteor every time, you will have to put your code into a Meteor.startup() function. If you simulate a browser, you could leave the meteor server running, and then specify arbitrary functions to execute based on forms or querystrings.
You can also do Meteor.methods({eval: function(code){eval(code);}})
, which allows you to type in whatever code you want and run it server-side. It's not the most secure thing in the world, but it worked for me. My guess is it's possible to make it somewhat less insecure by making sure that the user with this.userId in the method is an admin. Here's a repo for testing, feel free to clone and fork:
https://github./belisarius222/meteor-eval-test
DISCLAIMER: this is code that allows anyone to run arbitrary code on the server. It's a proof of concept, not intended to be secure whatsoever.