Specifying content-type in Meteor (JavaScript)

content-type, http, javascript, meteor

Solution

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.

Problem

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?

Original source

Related problems