srinivas
srinivas

Reputation: 5098

how to create a Rest API in Meteor

How can one design/write an efficient rest API with meteor, that can also be used by mobile apps? Can mobile apps also take advantage of meteors reactive programming?

With so many programming choices currently available, it seems wasteful to repeat everything(code, api) for different platforms, instead of having a good practical solution.

Upvotes: 3

Views: 4193

Answers (3)

kahmali
kahmali

Reputation: 557

To answer your first question, I published a package for writing REST APIs in Meteor 0.9+. The APIs can certainly be consumed by mobile apps:

https://github.com/krose72205/meteor-restivus

It was inspired by RestStop2 and built with Iron Router's server-side routing.

UPDATE: I just wanted to include a code example for anyone that finds this. This is the Restivus Quick Start example from the GitHub README:

Items = new Mongo.Collection 'items'

if Meteor.isServer

  # Global API configuration
  Restivus.configure
    useAuth: true
    prettyJson: true

  # Generates: GET, POST, DELETE on /api/items and GET, PUT, DELETE on
  # /api/items/:id for Items collection
  Restivus.addCollection Items

  # Generates: GET, POST on /api/users and GET, DELETE /api/users/:id for
  # Meteor.users collection
  Restivus.addCollection Meteor.users,
    excludedEndpoints: ['deleteAll', 'put']
    routeOptions:
      authRequired: true
    endpoints:
      post:
        authRequired: false
      delete:
        roleRequired: 'admin'

  # Maps to: /api/posts/:id
  Restivus.addRoute 'posts/:id', authRequired: true,
    get: ->
      post = Posts.findOne @urlParams.id
      if post
        status: 'success', data: post
      else
        statusCode: 404
        body: status: 'fail', message: 'Post not found'
    post:
      roleRequired: ['author', 'admin']
      action: ->
        post = Posts.findOne @urlParams.id
        if post
          status: "success", data: post
        else
          statusCode: 400
          body: status: "fail", message: "Unable to add post"
    delete:
      roleRequired: 'admin'
      action: ->
        if Posts.remove @urlParams.id
          status: "success", data: message: "Item removed"
        else
          statusCode: 404
          body: status: "fail", message: "Item not found"

Upvotes: 5

Jérémie Parker
Jérémie Parker

Reputation: 3174

To answer the mobile part of your question,

Meteor will integrate a packaging process to release your app as an iOS / Android app by the use of Cordova. You may want to check this video out : Meteor Devshop SF August 2014, it's a presentation of said Meteor features with live demo.

It is true that you will never come close to the raw performance of Native application, but maintenance costs and skill set required by developers on your project are nowhere near comparable either.

For the API part, it is possible to produce RESTful API with Meteor. Using iron-router severside routing is a possible option.

The Full Edition of the Discover Meteor Book has an Extra chapter dedicated to just that.

Upvotes: 1

Andrew Mao
Andrew Mao

Reputation: 36900

Your post is really two different questions.

  1. Yes, there is a way to attach REST endpoints to Meteor. You just write them as normal Node.js code using connect or Express and attach them to the WebApp.connectHandlers after pulling in the webapp package (meteor add webapp).

  2. Mobile apps can take advantage of reactivity by being implemented in Javascript. You can either access your app directly from a mobile browser, or just use PhoneGap/Cordova to wrap it in a "native" app container. As phones become more popular, this will probably be the default way to deploy apps versus writing many copies of the same app in different codebases.

Upvotes: 4

Related Questions