Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access Request Parameters in Meteor?

Tags:

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.

like image 613
user1568936 Avatar asked Aug 03 '12 15:08

user1568936


People also ask

How do I get request parameters?

Just look at getAttribute(String name) or getParameter(String name) .

What are request parameters?

Request parameters are used to send additional information to the server. A URL contains these parameters. There are two types of parameters: Query Parameter: These are appended to the end of the request URL, Query parameters are appended to the end of the request URL, following '?'

Can you pass parameters to a GET request?

Using the params property we can pass parameters to the HTTP get request. Either we can pass HttpParams or an object which contains key value pairs of parameters. Let's go through an example to understand it further.


1 Answers

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

Install Iron Router and define a server-side route:

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

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.

Original Post:

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

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

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.

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.

like image 87
Geoffrey Booth Avatar answered Oct 26 '22 13:10

Geoffrey Booth