How do I access Request Parameters in Meteor?
Asked Answered
E

5

19

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.

Endorsee answered 3/8, 2012 at 15:48 Comment(1)
I am not sure if it is possible to access the underlying node.js instance. But if you can somehow get hold of it, a quick app.post('/', function(request, response){}); would do the trick. Just an idea though, I really have no idea if this is possible.Psychopath
M
12

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.

Minded answered 13/5, 2013 at 6:35 Comment(2)
As of Iron Router >=1.0, this.request.query has been deprecated for this.params.queryPentomic
@Geoffrey-Booth I have been trying to send data from a GSM shield and this line of code has a GET I would just switch it for a POST although is there a need for a 'Meteor.call('POST'' or is Iron Router already deal with this? 'char request[ ]="GET /test-get-post.php?a=1&b=2 HTTP/1.1\r\nHost: test.libelium.com\r\nContent-Length: 0\r\n\r\n";'Delative
D
9

I found a workaround to add a router to the Meteor application to handle custom requests.

It uses the connect router middleware which is shipped with meteor. No extra dependencies!

Put this before/outside Meteor.startup on the Server. (Coffeescript)

SomeCollection = new Collection("...")
fibers = __meteor_bootstrap__.require("fibers")
connect = __meteor_bootstrap__.require('connect')
app = __meteor_bootstrap__.app

router = connect.middleware.router (route) ->
  route.get '/foo', (req, res) ->
    Fiber () ->
      SomeCollection.insert(...)
    .run()
    res.writeHead(200)
    res.end()
app.use(router)
Dierdredieresis answered 10/10, 2012 at 12:44 Comment(3)
This should be documented at docs.meteor.com (at least until a more official solution is out :) @GeoffRespectful
add the following if you want to use automatic query and body parsing: app.use(connect.query()).use(connect.bodyParser())Latinism
Note: as of 0.6.0 this won't work — though look at NPM.require('...');Rematch
H
2

Use IronRouter, it's so easy:

var path = IronLocation.path();
Hoagy answered 12/6, 2014 at 10:59 Comment(0)
T
0

As things stand, there isn't support for server side routing or specific actions on the server side when URLs are hit. So it's not easy to do what you want. Here are some suggestions.

  1. You can probably achieve what you want by borrowing techniques that are used by the oauth2 package on the auth branch: https://github.com/meteor/meteor/blob/auth/packages/accounts-oauth2-helper/oauth2_server.js#L100-109

    However this isn't really supported so I'm not certain it's a good idea.

  2. Your other applications could actually update the collections using DDP. This is probably easier than it sounds.

  3. You could use an intermediate application which accepts POST/GET requests and talks to your meteor server using DDP. This is probably the technically easiest thing to do.

Tonisha answered 6/8, 2012 at 3:8 Comment(2)
Actually, Meteor Router does handle server-side routes in addition to the more-common client-side ones. See the docs on its github page: github.com/tmeasday/meteor-routerMinded
Haha, you are right of course, but when I wrote this answer I hadn't yet added that functionality to the Router ;)Tonisha
S
-1

Maybe this one will help you? http://docs.meteor.com/#meteor_http_post

Sad answered 4/8, 2012 at 3:56 Comment(1)
this is to make an external http post, not on incoming ones.Taxis

© 2022 - 2024 — McMap. All rights reserved.