4

How can I specify content-type in Meteor?

I've got a page that returns JSON but response header is html/text I need to make it application/json. I am using iron-router and then the json is displayed through a template. I just need to change the response header for that page.

How can I do it?

user1952811
  • 2,398
  • 6
  • 29
  • 49
  • 1
    If you are using a server-side route, see my answer to [this question](http://stackoverflow.com/questions/21565991/how-to-serve-a-file-using-iron-router-or-meteor-itself). Just change the headers to whatever you need. – David Weldon May 14 '14 at 02:23
  • ^ That's it. If you want to write an answer for this question so I can accept it, that would be great :) – user1952811 May 14 '14 at 22:40

1 Answers1

6

Here's a simple example using a server-side route:

Router.map(function() {
  this.route('jsonExample', {
    where: 'server',
    path: '/json',
    action: function() {
      var obj = {cat: 'meow', dog: 'woof'};
      var headers = {'Content-type': 'application/json'};
      this.response.writeHead(200, headers);
      this.response.end(JSON.stringify(obj));
    }
  });
});

If you add that to your app and go to localhost:3000/json you should see the correct result.

David Weldon
  • 63,632
  • 11
  • 148
  • 146