How to expose a RESTful Web Service using Meteor

2020-01-27 00:20发布

How would you go about creating a restful web service using Meteor. I would like to create apps in Appcelerator that hook into the same backend.

Can Meteor solve this problem?

标签: rest meteor
9条回答
混吃等死
2楼-- · 2020-01-27 01:02

The most elegant solution appears to be HTTP.publish. Rather than invent a new API like the others, it simply adds the HTTP protocol to the existing Meteor publish interface. This means, for example, that Meteor.allow and Meteor.deny work automatically for HTTP as well as DDP.

Example:

If handed a collection and a publish function the HTTP.publish will mount on the following URLs and methods:

GET - /api/list - all published data
POST - /api/list - insert a document into collection
GET - /api/list/:id - find one published document
PUT - /api/list/:id - update a document
DELETE - /api/list/:id - remove a document

myCollection = new Meteor.Collection('list');

// Add access points for `GET`, `POST`, `PUT`, `DELETE`
HTTP.publish(myCollection, function(data) {
  // this.userId, this.query, this.params
  return myCollection.find({});
});

It does not yet handle authentication completely.

查看更多
一夜七次
3楼-- · 2020-01-27 01:03

I originally answered this question here, but to recap:

For adding RESTful methods on top of your data, look into the Collection API written for Meteor:

https://github.com/crazytoad/meteor-collectionapi

As for authentication for accessing the database, take a look at this project:

https://github.com/meteor/meteor/wiki/Getting-started-with-Auth

Both are definitely infantile in development, but you can create a RESTful API and integrate it with a mobile native client pretty easily.

查看更多
三岁会撩人
4楼-- · 2020-01-27 01:06

I did a full write-on on this in Meteorpedia:

http://www.meteorpedia.com/read/REST_API

The post reviews all 6 options for creating REST interfaces, from highest level (e.g. smart packages that handle everything for you) to lowest level (e.g. writing your own connectHandler).

Additionally the post covers when using a REST interface is the right or wrong thing to do in Meteor, references Meteor REST testing tools, and explains common pitfalls like CORS security issues.

查看更多
登录 后发表回答