falcor README

The project's README from github.com/Netflix/falcor. As one markdown file: README.md.

Falcor

Build Status Coverage Status

2.0

2.0 is the current stable Falcor release. 0.x and 1.x users are welcome to upgrade.

Roadmap

Issues we're tracking as part of our roadmap are tagged with the roadmap label. They are split into enhancement, stability, performance, tooling, infrastructure and documentation categories, with near, medium and longer term labels to convey a broader sense of the order in which we plan to approach them.

Getting Started

You can check out a working example server for Netflix-like application right now. Alternately, you can go through this barebones tutorial in which we use the Falcor Router to create a Virtual JSON resource. In this tutorial we will use Falcor's express middleware to serve the Virtual JSON resource on an application server at the URL /model.json. We will also host a static web page on the same server which retrieves data from the Virtual JSON resource.

Creating a Virtual JSON Resource

In this example we will use the falcor Router to build a Virtual JSON resource on an app server and host it at /model.json. The JSON resource will contain the following contents:

{
  "greeting": "Hello World"
}

Normally, Routers retrieve the data for their Virtual JSON resource from backend datastores or other web services on-demand. However, in this simple tutorial, the Router will simply return static data for a single key.

First we create a folder for our application server.

$ mkdir falcor-app-server
$ cd falcor-app-server
$ npm init

Now we install the falcor Router.

$ npm install falcor-router --save

Then install express and falcor-express. Support for restify is also available, as is support for hapi via a third-party implementation.

$ npm install express --save
$ npm install falcor-express --save

Now we create an index.js file with the following contents:

// index.js
const falcorExpress = require("falcor-express");
const Router = require("falcor-router");

const express = require("express");
const app = express();

app.use(
    "/model.json",
    falcorExpress.dataSourceRoute(function (req, res) {
        // create a Virtual JSON resource with single key ('greeting')
        return new Router([
            {
                // match a request for the key 'greeting'
                route: "greeting",
                // respond with a PathValue with the value of 'Hello World.'
                get: () => ({ path: ["greeting"], value: "Hello World" }),
            },
        ]);
    })
);

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

app.listen(3000);

Now we run the server, which will listen on port 3000 for requests for /model.json.

$ node index.js

Retrieving Data from the Virtual JSON resource

Now that we've built a simple virtual JSON document with a single read-only key greeting, we will create a test web page and retrieve this key from the server.

Create an index.html file with the following contents:

<!-- index.html -->
<html>
    <head>
        <!-- Do _not_  rely on this URL in production. Use only during development.  -->
        <script src="https://netflix.github.io/falcor/build/falcor.browser.js"></script>
        <!-- For production use. -->
        <!-- <script src="https://cdn.jsdelivr.net/falcor/{VERSION}/falcor.browser.min.js"></script> -->
        <script>
            var model = falcor({
                source: new falcor.HttpDataSource("/model.json"),
            });

            // retrieve the "greeting" key from the root of the Virtual JSON resource
            model.get("greeting").then(function (response) {
                document.write(response.json.greeting);
            });
        </script>
    </head>
    <body></body>
</html>

Now visit http://localhost:3000/index.html and you should see the message retrieved from the server:

Hello World

Steps to publish new version

Additional Resources

falcor (Netflix/falcor)

Searching Database…

10.6K ·GitHub
这是什么?Falcor 是 Netflix 推出的 JavaScript 库,用于高效获取数据。它让你把后端的所有数据建模为一个虚拟的 JSON 图(JSON Graph),客户端可以按需逐段查询,而不必调用多个 REST 接口。
它是客户端库、服务端框架,还是两者兼有?它是一个两端通用的库:客户端使用 Model API 查询 JSON Graph;服务端使用 Router(配合 Express/Hapi/Restify 中间件)把这份虚拟 JSON 解析为真实数据。
如何运行一个基础的 Falcor 服务端和客户端?安装 falcor 的 Router 以及 express 和 falcor-express,编写一个 index.js 来构建 Router,并在 /model.json 上提供服务;然后创建 index.html,使用 Falcor Model 通过 HTTP 从该端点获取所需的键。
它底层依赖什么?库本身依赖 falcor-json-graph、falcor-path-syntax、falcor-path-utils 和 symbol-observable;常见的搭配还会添加 express(或 hapi/restify)以及 falcor-express 作为服务端传输层。
升级或采用它时有哪些需要注意的地方?0.x、1.x 与当前的 2.0 版本之间存在不兼容的改动,因此跨大版本升级需要查看更新日志,不能直接替换。
这个项目主要面向谁?面向构建数据密集型应用(例如 Netflix 自家界面)的 JavaScript/Node 后端与前端开发者,他们希望使用统一的数据模型,而不是大量定制的 REST 调用。
什么是 JSON Graph?JSON Graph 是 Falcor 用来表示应用全部远程数据集的虚拟 JSON 文档模型,Router 会按需从后端数据存储或其他服务中构建它。
Falcor 如何避免经典 REST 的过度获取问题?客户端不使用固定接口,而是在共享的 JSON Graph 中请求特定路径,Router 只解析这些路径,因此请求返回的正是所需数据,并尽可能减少往返次数。
在哪里可以看到可运行的示例?有一个为类似 Netflix 应用而构建的示例服务端,另外还有单独的 falcor-router-demo,用于演示 Router 的运行效果。
在哪里提问关于 Falcor 的问题?README 中建议用户前往 Stack Overflow 提问和讨论。
What are similar projects?express, koa, pollyjs, multer, node-restify, ky, fiber, pino
Creator
Netflix
Size
23.1 MB
Stars
10.6K
Updated
CommitAug 28
Age
11y
Commits · 90 Days