2

私は CompoundJS を初めて使用しますが、これが正しい動作であるかどうかはよくわかりません。

次のコードを使用して、サーバーを PROD モードで起動しています。

NODE_ENV=production compound server 8081

それから私はヒットしました:

http://localhost:8081/categories/

サーバーからJSONが取得されているのを見るのは例外でした。

代わりに、次のようなページをレンダリングします。

ここに画像の説明を入力

4

1 に答える 1

2

コメントで@Pabloが述べたよう.jsonに、コントローラーへの呼び出しで使用するだけです。

次のように:

GET http://localhost:3000/categories.json

生成されたコントローラーの場合と同様に、コントローラーは両方を処理することが期待されます。

具体例:[approot]/app/controllers/category_controller.js

JavaScript の場合:

action(function index() {
    this.title = 'Categories index';
    Category.all(function (err, categories) {
        respondTo(function (format) {
            // Use format.json and the send method to return JSON data when 
            // .json is specified at the end of the controller
            format.json(function () {
                send({code: 200, data: categories});
            });
            format.html(function () {
                render({
                   categories: categories
                });
            });
        });
    });
});

CoffeeScript では:

action index = ->
  @title = "Categories index"
  Category.all (err, categories) ->
    respondTo (format) ->

      # Use format.json and the send method to return JSON data when 
      # .json is specified at the end of the controller
      format.json ->
        send
          code: 200
          data: categories


      format.html ->
        render categories: categories
于 2013-06-07T22:02:28.440 に答える