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

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

问题描述

我计划将 Meteor 用于各种实时日志记录应用程序我的要求非常简单,我将传递一个日志消息作为来自各种应用程序的请求参数(POST 或 GET),而 Meteor 需要简单地更新一个集合.我需要访问 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.

推荐答案

更新为使用 Iron Router,流星路由器的后继者.

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.

原帖:

使用 Meteorite 安装 Meteor Router 并定义服务器端路由:

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.

我知道提问者不想添加包,但我认为使用 Meteorite 安装 Meteor Router 在我看来,与访问内部未记录的 Meteor 对象(如 __meteor_bootstrap__.当包 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.

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

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