iron-meteor / iron-router

A client and server side router designed specifically for Meteor.
MIT License
1.98k stars 412 forks source link

Route params in server routes #1360

Open urossmolnik opened 9 years ago

urossmolnik commented 9 years ago

params property on route callback is not se correctly for server routes?

For the example below, when going to http://localhost:3000/echo/asdf this.params is set to [ 'echo/asdf', hash: null, query: {} ].

As understood from documentation params should be something like { msg: 'asdf', hash: null, query: {} }

Example:

Router.map(function() {
  this.route('/echo/:msg', {
    name: 'echo',
    where: 'server',
    action: function() {
      this.response.writeHead(200, {'Content-Type': 'text/plain'});
      this.response.end(this.params.msg || '');
    }
  });
});

I am using iron:router@1.0.7

fvilers commented 9 years ago

I think you have a scope issue with this. I'll try:

Router.map(function() {
  var self = this;
  this.route('/echo/:msg', {
    name: 'echo',
    where: 'server',
    action: function() {
      this.response.writeHead(200, {'Content-Type': 'text/plain'});
      this.response.end(self.params.msg || '');
    }
  });
});
urossmolnik commented 9 years ago

Not working either. self.params is undefined for given example.

fvilers commented 9 years ago

Give a try with the route method instead of map:

Router.route('/echo/:msg', function() {
    this.response.writeHead(200, {'Content-Type': 'text/plain'});
    this.response.end(this.params.msg || '');
}, {
    name: 'echo',
    where: 'server'
});