How do I access Request Parameters in Meteor?

后端 未结 5 539
天命终不由人
天命终不由人 2020-12-14 02:34

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) f

相关标签:
5条回答
  • 2020-12-14 03:02

    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.

    0 讨论(0)
  • 2020-12-14 03:03

    Use IronRouter, it's so easy:

    var path = IronLocation.path();
    
    0 讨论(0)
  • 2020-12-14 03:10

    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)
    
    0 讨论(0)
  • 2020-12-14 03:12

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

    0 讨论(0)
  • 2020-12-14 03:19

    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.

    0 讨论(0)
提交回复
热议问题