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