Skip to main content

Publish an API to a MongoDB collection using Meteor in 5 minutes

Suppose you have a collection in your MongoDB database and want to publish it over a REST API. This can be done in minutes using Meteor. Follow the steps.

Create the Meteor project

Open the command-line and type:

meteor create my_project
cd my_project

Connect to the collection

Suppose you want to publish the collection named collection_name from your MongoDB database.

Just add the following line of code to the my_project.js file:

MyCollection = new Mongo.Collection("collection_name");

Add the Restivus package

Open the command-line and type:

meteor add nimble:restivus

Publish the API

Add the following line of code to the my_project.js file:

if(Meteor.isServer) {
  Meteor.startup(function () {
    var api = new Restivus({ });
    api.addCollection(MyCollection);
  });
}

Enjoy the result

You can now use the following REST API:

  • GET, POST on /api/collection_name
  • GET, PUT, DELETE on /api/collection_name/:id

Wait...

Best of all is that normally, in your Meteor project, you would already have written the new Mongo.Collection() line. So basically all that it takes is two lines of JavaScript. Wow! Did I tell you that I love Meteor?

Comments

Popular posts from this blog

Learn Meteor book available

I'm pleased to announce the general release of my Learn Meteor book. It is now available as an ebook or print book from various sources: Learn Meteor print (paperback) on Lulu Learn Meteor ebook on LeanPub Learn Meteor ebook on Barnes & Noble Learn Meteor ebook on iBooks Learn Meteor ebook on Kobo Learn Meteor ebook on Scribd Learn Meteor ebook on Inktera Page Foundry Learn Meteor ebook on 24symbols Learn Meteor ebook on Amazon US Learn Meteor ebook on Amazon UK Learn Meteor ebook on Amazon France Learn Meteor ebook on Amazon Deutschland Learn Meteor ebook on Amazon Canada Learn Meteor ebook on Amazon India Learn Meteor ebook on Amazon Brasil Learn Meteor ebook on Amazon Mexico Learn Meteor ebook on Amazon España Learn Meteor ebook on Amazon Italia Learn Meteor ebook on Amazon Netherlands Learn Meteor ebook on Amazon Japan Learn Meteor ebook on Amazon Australia More sources are coming soon for the print version. Learn Meteor has been a fun experienc