Having trouble getting falcor router to reference data using an external API through sidecar

284 views Asked by At

I have set-up a few Netflix OSS microservices (Eureka, Falcor sidecar) as well as a very basic Falcor set-up in order to test the functionality. I am trying to make a call from the client-side of Falcor to an external API, whose endpoint is http://localhost:8001/customer/customers. In my index.js file I have:

app.use('/model.json', falcorExpress.dataSourceRoute(function (req, res) {
return new falcorRouter([
    {
        route: "customers.length",
        get: function (pathset) {
            return http.get("http://localhost:8001/customer/customers")
                .then(function (json) {
                    return {
                        path: pathset,
                        value: $atom(json.length)
                    };
                });
        }
    }
   ]);
}));

Then in my client-side index.html:

<html>
 <head>
  <script src="js/falcor.browser.js"></script>
  <script>
    var model = new falcor.Model({source: new falcor.HttpDataSource('/model.json') });
    model.
    get("customers.length").
    then(function(response) {
        console.log(response);
    });
  </script>
 </head>
<body>
</body>
</html>

If I hit the http://localhost:8001/customer/customers API manually, I get back a JSON object like this:

[
  {
    "customerId": 1,
    "customerName": "Pierre"
  },
  {
    "customerId": 2,
    "customerName": "Paula"
  }
]

However, my console.log is outputting an error object like this:

[{path: ["customers","length"],
  value: {message: "undefined is not a function"}}]

I am not too interested at this stage in exactly what format object I get back, I just want something to play with. How do I amend my router to get the data I am expecting back?

Thanks

1

There are 1 answers

0
ashutosh On

You should try this...

SERVER

    // index.js
var falcorExpress = require('falcor-express');
var Router = require('falcor-router');

var express = require('express');
var app = express();
var fs = require("fs");
var q = require("q");

app.use('/model.json', falcorExpress.dataSourceRoute(function(req, res) {
    var jdata;

    function getJson() {
        var defer = q.defer();
        fs.readFile(__dirname + "/" + "abc.json", 'utf8', function(err, data) {
            // console.log(data);
            if (err) {
                defer.reject(err);
            } else {
                jdata = JSON.parse(data);
                defer.res
                defer.resolve(jdata);
            }
        });
        return defer.promise;
    }

    return new Router([
        {
            // match a request for the key "falcor.greeting"    
            route: "falcor.greeting",
            get: function() {
                return { path: ["falcor", "greeting"], value: "hello" };
            }
        },
        {
            // match a request for the key "fa.greeting"
            route: 'fa.greeting',
            get: function() {
                return getJson().then(function(data) {
                    console.log(data.fa.greeting);
                    return { path: ["fa", "greeting"], value: data.fa.greeting };
                }, function(error) {
                    console.log(error);
                    return error;
                });
            }
        },
        {
            // match a request for the key "sa.ga"
            route: 'sa.ga',
            get: function() {
                return getJson().then(function(data) {
                    console.log(data.sa.ga);
                    return { path: ["sa", "ga"], value: data.sa.ga };
                }, function(error) {
                    console.log(error);
                    return error;
                });
            }
        }
    ]);
}));

// serve static files from current directory
app.use(express.static(__dirname + '/'));

var server = app.listen(8008, function(err) {
    if (err) {
        console.error(err);
        return;
    }
    console.log("navigate to http://localhost:8008");
});

CLIENT

<!-- index.html -->
<html>

<head>
    <!-- Do _not_  rely on this URL in production. Use only during development.  -->
    <script src="//netflix.github.io/falcor/build/falcor.browser.js"></script>
    <script>
        var model = new falcor.Model({
            source: new falcor.HttpDataSource('/model.json')
        });

        //logging:
        var log = console.log.bind(console);
        var jlog = function(x) {
            console.log(JSON.stringify(x, null, 3));
        };
        var jerror = function(x) {
            console.error(JSON.stringify(x, null, 3));
        };


        model.get("falcor.greeting").then(jlog, jerror)

        model.get("fa.greeting").then(jlog, jerror)

        model.get("sa.ga").then(jlog, jerror)
    </script>
</head>

<body>
</body>

</html>

JSON FILE

{
    "fa": {
        "greeting": "Hello World"
    },
    "sa": {
        "ga": "ha"
    }
}

OUTPUT ON BROWSER'S CONSOLE

{
   "json": {
      "falcor": {
         "greeting": "hello"
      }
   }
}
(index):15 {
   "json": {
      "fa": {
         "greeting": "Hello World"
      }
   }
}
(index):15 {
   "json": {
      "sa": {
         "ga": "ha"
      }
   }
}