MeanJS - Expose data as JSON via API

By peterm, 27 April, 2015

One of my objectives with migrating data out of Drupal and into a MEAN stack app is for easier management of downstream requests for JSON data. We want to take advantage of the built in functionality of the MeanJS stack, and here's a simple exaple.

In this use case we wish to expose articles to some other app who will make a HTTP GET request for all the articles or just one.

In app/controllers/articles.server.routes.js, we need to add entries for the two functions we'll allow via GET.

app.route('/api/articles')

.get(articles.list);

app.route('/api/articles/:articleId')

.get(articles.read);

Save this change and use the URL localhost:3000/api/articles or localhost:3000/aticles/<your article _id here>.

[{"_id":"553940bcbfadec1124c93082","user":{"_id":"553818ad7e5443733ddda340","displayName":"Peter McMillan"},"__v":0,"content":"article 1","title":"article 1","created":"2015-04-23T18:58:04.321Z"}]