javascriptnode.jscompoundjs

CompoundJS in production mode renders webpage instead of retrieving JSON data


I'm new to CompoundJS and I'm not too sure if this is the correct behaviour.

I'm starting the server on PROD mode with the following code:

NODE_ENV=production compound server 8081

Then I hit:

http://localhost:8081/categories/

I was excepting to see some JSON being retrieved from the server.

Instead, it renders a page like this:

enter image description here


Solution

  • As @Pablo mentioned in the comments, just use .json in the call to your controller.

    As in the following:

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

    It is expected that your controller will handle both, as is the case in generated controllers.

    A specific example: [approot]/app/controllers/category_controller.js

    In 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
                    });
                });
            });
        });
    });
    

    In 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