How to make restful api using meteorjs

#1

I want to consume data in my android mobile app, but i only found iron router to make server side routing, What I need is make a restful API so my android app can consume data or post data to meteor app

thanks

– EDIT

much developer suggest to not use iron router again, so is there any solution?

#2

There are various libraries. I’m using mrest:restivus, and that seems to work so far? I haven’t used it super intensively yet though, so we’ll see. :confused:

#3

After fighting with restivus (no update since 2 years, we can consider near dead ?) to try to work with AdminOnRest - unsuccessfully configuring the authClient / restivusClient (I gave up debugging this tricky part…), I also used for simple post upload, Picker (which is also almost dead as maintained by former meteor’s guru “Arunoda the great”, glory to him). Amazingly working fine with 1.6…

1 Like
#4

You don’t need to install any packages to enable this functionality, it’s built into Meteor. See the webapp package documentation here

If you look at the source code for Picker, the main functionality it’s adding is different types of route parsing, and using a WebApp.raw… etc method to create the listener. A similar minimal starting point would be checking zeit.co’s micro server library, and how users add expected features like routing, body parsing etc to a barebones server.

Quoted from the docs:

This package also allows you to add handlers for HTTP requests. This lets other services access your app’s data through an HTTP API, allowing it to easily interoperate with tools and frameworks that don’t yet support DDP

// Listen to incoming HTTP requests (can only be used on the server).
WebApp.connectHandlers.use('/hello', (req, res, next) => {
  res.writeHead(200);
  res.end(`Hello world from: ${Meteor.release}`);
})
1 Like
#5

The most common addition is parsing the request to get the json body etc, here is an async/await starter boilerplate (not tested) basic example. You’ll need a body-parsing lib and add that code in.

// import body-parsing lib


const getJson = (req, options = {}) =>
  new Promise((resolve, reject) => {
    try {
      // use a lib body-parser/busboy etc
      return resolve(// body result)
    } catch (e) {
      // handle error from body parsing lib etc.
      return reject(e)
    }
  })

WebApp.connectHandlers.use('/hello', async (req, res, next) => {

  const json = await getJson(req)

  // do something with json/body

  res.writeHead(200);
  res.end(`Hello world from: ${Meteor.release}`);
})
4 Likes
#6

I uploaded an example repo that works.

3 Likes
Adapting Chargebee API for use with Meteor