Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON endpoint in Meteor

Tags:

meteor

Is there a way to return straight text in a page using meteor? Say someone requested domain.com/get/that-thing, and I just wanted to return the string "52", so that the requester knows that-thing has "52" of something. To my understanding, this is not possible in Meteor because the headers and such are always included.

2 hacks that would work: Write to a file named "that-thing" in anticipation that "that-thing" might be called. This doesn't work in the general case. Put a reverse proxy that redirects some of the requests to a non-meteor backend.

Is there a better way to do this?

like image 228
theicfire Avatar asked Mar 24 '13 16:03

theicfire


1 Answers

I had to solve this today and using Iron-Router server-side-routing: https://github.com/EventedMind/iron-router/blob/master/DOCS.md#server-side-routing

Simple example:

Router.map(function () {
  this.route('api', {
    path: '/api',
    where: 'server',
    action: function () {
      var json = Collection.find().fetch(); // what ever data you want to return
      this.response.setHeader('Content-Type', 'application/json');
      this.response.end(JSON.stringify(json));
  }
});
});

This will return a valid JSON "page" which you can then use how ever you want.

Thanks to @Akshat for answering: Meteor Iron-Router Without Layout Template or JSON View

like image 94
nelsonic Avatar answered Oct 08 '22 14:10

nelsonic