如何使用 Meteor 设计/编写一个高效的 REST API,并且该 API 也可以被移动应用程序使用?移动应用程序也可以利用流星响应式编程吗?
目前有如此多的编程选择,为不同的平台重复所有内容(代码、API)似乎很浪费,而不是拥有一个良好的实用解决方案。
为了回答你的第一个问题,我发布了一个用于在 Meteor 0.9+ 中编写 REST API 的包。这些 API 当然可以被移动应用程序使用:
https://github.com/krose72205/meteor-restivus
它受到RestStop2的启发,并使用Iron Router的服务器端路由构建。
更新:我只是想为找到此内容的任何人提供一个代码示例。这是 GitHub README 中的 Restivus 快速入门示例:
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"
Run Code Online (Sandbox Code Playgroud)