JSON Endpoint in Meteor

Is there a way to return direct text on a page using a meteorite? Say someone requested domain.com/get/that -thing, and I just wanted to return the string “52” so that the interrogator knew that the thing had “52” something. As far as I understand, this is not possible in Meteor, because headers, etc. Always included.

2 hacks that will work: Write to a file called "that-thing" in anticipation that "this thing" might be called. This does not work in the general case. Put a reverse proxy server that redirects some of the requests to the non-meteor backend.

Is there a better way to do this?

+6
source share
2 answers

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

0
source

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

A 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 use whenever you want.

Thanks to @Akshat for the answer: Meteor Iron-Router without a template template or JSON View

+13
source

All Articles