如何访问流星中的请求参数? [英] How do I access Request Parameters in Meteor?

查看:62
本文介绍了如何访问流星中的请求参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划将Meteor用于各种实时记录应用程序 我的要求非常简单,我将传递来自各种应用程序的日志消息作为请求参数(POST或GET),而Meteor需要简单地更新集合. 我需要访问流星服务器代码中的请求参数,并使用传入的logMessage更新Mongo集合.我无法直接从现有应用程序中更新Mongo Collection,因此请不要提出任何建议.我想知道如何从Meteor框架中进行操作,而不是通过添加更多软件包来进行操作.

I am planning to use Meteor for a realtime logging application for various My requirement is pretty simple, I will pass a log Message as request Parameter ( POST Or GET) from various application and Meteor need to simply update a collection. I need to access Request Parameters in Meteor server code and update Mongo collection with the incoming logMessage. I cannot update Mongo Collection directly from existing applications, so please no replies suggesting the same.I want to know how can I do it from Meteor framework and not doing it by adding more packages.

推荐答案

编辑:已更新为使用铁路由​​器,流星路由器的后继者.

Updated to use Iron Router, the successor to Meteor Router.

安装Iron Router 并定义服务器端路由:

Install Iron Router and define a server-side route:

Router.map(function () {
  this.route('foo', {
    where: 'server',
    action: function () {
      doSomethingWithParams(this.request.query);
    }
  });
});

因此,对于像http://yoursite.com/foo?q=somequery&src=somesource这样的请求,上面函数中的变量this.request.query将是{ q: 'somequery', src: 'somesource' },因此您可以通过this.request.query.qthis.request.query.src等请求单个参数.我只测试了GET请求,但是POST和其他请求类型应该完全相同.从Meteor 0.7.0.1开始有效.确保将此代码放在项目的Meteor.isServer块中或/server文件夹中的文件中.

So for a request like http://yoursite.com/foo?q=somequery&src=somesource, the variable this.request.query in the function above would be { q: 'somequery', src: 'somesource' } and therefore you can request individual parameters via this.request.query.q and this.request.query.src and the like. I've only tested GET requests, but POST and other request types should work identically; this works as of Meteor 0.7.0.1. Make sure you put this code inside a Meteor.isServer block or in a file in the /server folder in your project.

原始帖子:

使用陨石安装流星路由器,并定义服务器端路由:

Use Meteorite to install Meteor Router and define a server-side route:

Meteor.Router.add('/foo', function() {
  doSomethingWithParams(this.request.query);
});

因此,对于像http://yoursite.com/foo?q=somequery&src=somesource这样的请求,上面函数中的变量this.request.query将是{ q: 'somequery', src: 'somesource' },因此您可以通过this.request.query.qthis.request.query.src等请求单个参数.我只测试了GET请求,但是POST和其他请求类型应该完全相同.从Meteor 0.6.2.1开始有效.确保将此代码放在项目的Meteor.isServer块中或/server文件夹中的文件中.

So for a request like http://yoursite.com/foo?q=somequery&src=somesource, the variable this.request.query in the function above would be { q: 'somequery', src: 'somesource' } and therefore you can request individual parameters via this.request.query.q and this.request.query.src and the like. I've only tested GET requests, but POST and other request types should work identically; this works as of Meteor 0.6.2.1. Make sure you put this code inside a Meteor.isServer block or in a file in the /server folder in your project.

我知道发问者不想添加软件包,但我认为使用陨石安装__meteor_bootstrap__)相比,"https://github.com/tmeasday/meteor-router> Meteor Router 在我看来似乎是一种更加面向未来的方法来实现.当Package API在Meteor的将来版本中完成时,安装Meteor Router的过程将变得更加容易(不需要Meteorite),但其他任何事情都可能不会改变,并且您的代码可能会继续工作而无需进行修改.

I know the questioner doesn't want to add packages, but I think that using Meteorite to install Meteor Router seems to me a more future-proof way to implement this as compared to accessing internal undocumented Meteor objects like __meteor_bootstrap__. When the Package API is finalized in a future version of Meteor, the process of installing Meteor Router will become easier (no need for Meteorite) but nothing else is likely to change and your code would probably continue to work without requiring modification.

这篇关于如何访问流星中的请求参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆