JSON endpoint in Meteor
Asked Answered
V

2

7

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?

Verditer answered 24/3, 2013 at 16:51 Comment(0)
V
0

Router supports this; check out the server side routing: https://github.com/tmeasday/meteor-router

Verditer answered 24/3, 2013 at 17:41 Comment(1)
meteor-router is officially discontinued. the meteor-router GitHub repo points users to github.com/EventedMind/iron-router which does not support JSON. :-( writing a custom solution. Let me know if you want me to share)Pigment
P
14

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

Pigment answered 7/4, 2014 at 22:29 Comment(0)
V
0

Router supports this; check out the server side routing: https://github.com/tmeasday/meteor-router

Verditer answered 24/3, 2013 at 17:41 Comment(1)
meteor-router is officially discontinued. the meteor-router GitHub repo points users to github.com/EventedMind/iron-router which does not support JSON. :-( writing a custom solution. Let me know if you want me to share)Pigment

© 2022 - 2024 — McMap. All rights reserved.