简体   繁体   中英

Send data in Meteor HTTP GET request

Quick question: How can I send data to GET request using iron server side routes?

Router.route( "/api/test", function() {
  this.response.writeHead(200, {
    'Access-Control-Allow-Origin': '*'
  });
  this.response.statusCode = 200;
  this.response.data = {test: 'test'};
  this.response.end('end');
}, {where: 'server'});

See the 2 examples under Sending a response section of MeteorChef Website,

Router.route( "users/:name/profile", function() {
  var name   = this.params.name,
      query  = this.request.query,
      fields = {};

  fields[ query.field ] = query.field;

  var getUser = Meteor.users.findOne( { "profile.username": name }, { fields: fields } ); 

  if ( getUser ) {
      this.response.statusCode = 200;
      this.response.end( getUser.profile );
  } else {
      this.response.statusCode = 404;
      this.response.end( { status: "404", message: "User not found." } );
  }
}, { where: "server" });

So you can send your data using this.response.end like this,

Router.route( "/api/test", function() {
  this.response.writeHead(200, {
    'Access-Control-Allow-Origin': '*'
  });
  this.response.statusCode = 200;
  //this.response.data = {test: 'test'};
  this.response.end({ test: 'test' });
}, {where: 'server'});

I have never tried server side routes myself, So I am not sure whether it works or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM