0

我想使用流星制作一个简单的 API 来使用。

这样像 www.myapp.com/artist/id 这样的 URL 会返回我在服务器上所做的一些工作的 JSON 响应。

我不确定使用流星(也许是骨干)的最佳方法。

我知道我会使用 Meteor.http.get("url") 但不太确定我应该如何定义 api 的路由。

还是最好在不同的环境中制作api,然后调用它?

4

3 回答 3

2

您可以使用 https://atmosphere.meteor.com/package/collection-api通过 RESTful API 对集合执行 CRUD 操作

于 2012-11-21T03:27:33.810 回答
1

At the moment, Meteor does not support server-side routing (Little/big bird telling me it is on the roadmap). Though with some hacky work around you can achieve it. Although if you want to keep clean code and stay away from the hacky stuff a external system might be the better choice here. BUT let's stay Meteor minded and 'hack'-away.

A server-side route can be achieved by using this code :

var connect = __meteor_bootstrap__.require("connect");
__meteor_bootstrap__.app
.use(connect.query())
.use(connect.bodyParser()) //I add this for file-uploading
.use(function (req, res, next) {
     Fiber(function() {
        if(req.method == "POST"){
           if(req.url.indexOf('/upload') !== -1){
             res.writeHead(200, {'Content-Type': 'application/json'});  
             res.write(JSON.stringify({"success" : true}));
             res.end();
             return;
           }
        }       
      next();
  }).run();
});
于 2012-11-13T00:15:05.133 回答
0

您可以使用page.js来帮助您进行路由。Meteor & Backbone.js 在模型/集合和视图/模板中有一些非常相似的功能。

于 2012-11-10T21:57:52.363 回答