最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - While the meteor app is running, what are some ways of executing arbitrary code on the server side? - Stack Overflo

programmeradmin6浏览0评论

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:

  1. running using the meteor mand
  2. 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:

  1. running using the meteor mand
  2. 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 badges
Add a ment  | 

4 Answers 4

Reset to default 7

Couldn'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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论