1

I am building a Meteor app that needs to be able access request data and take action accordingly. My goal is to be able to get GET/POST data on both the server side and the client side. This is what I have so far:

The code I have above works on the server, but not on the client. Any suggestions? Thanks!

Update for clarity (9/5/13)

If I start up my Meteor app, open the browser at http://localhost:3000/?foo=bar, I want to be able to access foo on the server AND on the client using the same API.

if (Meteor.isServer) {
    var foo = RequestData.get('foo');
    console.log(foo);
}

if (Meteor.isClient) {
    var foo = RequestData.get('foo');
    console.log(foo);
}

Does anyone have any ideas how I can accomplish this, whether it's modifying the code I already have, or starting over? Thanks!

Update on progress (9/5/13)

Thanks to @Denis for suggesting I use window.location.search to read GET data on the client. I took this SO answer and adopted it to fit my needs for this Meteor package. The request-data.js file posted above has been updated.

Now if I can just find a clean way to get the POST data to be available on the client. Thanks for the suggestions!

Community
  • 1
  • 1
John
  • 3,866
  • 6
  • 33
  • 37
  • What could be possible is to have an AJAX request sent to Page2 from Page1 that server side code do stuff with and then another AJAX call from Page2 to Page1 were this time Page1 act like the server. – Sebastien Sep 04 '13 at 20:11
  • Exemple: `";}?>` – Sebastien Sep 04 '13 at 20:15
  • Do the same thing but change var1 for var2, id="1" for id="2" and action="Page1.php" for Page2.php – Sebastien Sep 04 '13 at 20:16
  • On client side you have CORS. There are severtal methods to avoid it, but it more hacky tricks rather then solution, so you can create server methods which interact with external api and send data back to client. – Denis Sep 05 '13 at 11:55
  • @Denis That's what I'm trying to do with `Meteor.methods` and `Meteor.call`. – John Sep 05 '13 at 12:56
  • So on the server side you can use http module http://docs.meteor.com/#http and do requests to an external service. – Denis Sep 05 '13 at 13:14
  • @Denis I think I didn't phrase my question well enough... I am not try ing to make a GET/POST request from my Meteor app to an external resource, but the reverse, kind of. This is what I want: If I start up my Meteor app, open the browser at `http://localhost:3000/?foo=bar`, I want to be able to access `foo` on the server AND on the client. I'll update my question to reflect this. Thanks! – John Sep 05 '13 at 15:45
  • @Johnny So if you using router (for example IronRouter) you can get params from it and set them in Session. Or you can get params from standard javascript attribute window.location.search – Denis Sep 05 '13 at 15:50
  • @Denis I tried IronRouter specifically for this reason I'm posting about. Unfortunately, it is not yet compatible with browsers that don't support push state (see [this issue](https://github.com/EventedMind/iron-router/issues/14)) so I can't use it. :( – John Sep 05 '13 at 15:56
  • @Denis Didn't think about checking `window.location.search`... hmmm... so that covers GET data. Now how could I get POST data? – John Sep 05 '13 at 15:58
  • @Johnny Looks I feel you don't understand meteor's conception clean. On the client you could store your data at Session. If you have big volume of data you could use unnamed minimongo collections. All these sources are reactive. So, for example, you have IronRouter at you project you could trigger functions when customer use one of your paths and set data to Session. – Denis Sep 06 '13 at 07:28
  • @Denis I completely agree that I don't yet fully understand Meteor. However, request data can change per each request so storing the data in `Sessions` doesn't really make sense (as `Sessions` are for storing data that you want to remember across multiple requests, like app state). And storing request data in a Meteor Collection (named or unnamed) is bit overkill as the data doesn't change in the middle of a request (so it doesn't need `insert()`, `update()`, `remove()`, etc) and doesn't need to be reactive. I think using a object literal is a much smarter approach here. – John Sep 06 '13 at 14:14
  • @Denis And THANK YOU so much for helping me out with this! :) – John Sep 06 '13 at 14:16

0 Answers0