Giter VIP home page Giter VIP logo

featureflow-example-nodejs-client-server's Introduction

featureflow-example-nodejs-client-server

An example of using the featureflow nodejs SDK to return a list of evaluated features to a react frontend.

Typically you might use this setup if:

  1. You do not need the advanced features of the featureflow Javascript client
  2. You want the security of evaluating features on your own servers
  3. You want the cost-benefit of using server-side feature evaluations only.

Usage

Set your featureflow Server Environment SDK Key in server.js:

const API_KEY = 'srv-env-685...';

Install server and client dependencies

yarn
cd client
yarn

To start the server and client at the same time (from the root of the project)

yarn dev

Detail

This is based on the nodejs example https://github.com/featureflow/featureflow-node-example

  1. in server.js we create a middleware to define the user - this would typically be set from your logged in user details
let userMiddleware = function (req, res, next) {
  req.ffUser = new Featureflow.UserBuilder("[email protected]")
      .withAttribute("firstName", "Jimmy")
      .withAttribute("lastName", "Hendrix")
      .withAttributes("hobbies", ["swimming", "skiing", "rowing"])
      .withAttribute("age", 32)
      .withAttribute("signupDate", new Date(2017, 10, 28))
      .withAttribute("ip", req.ip)
      .build();
  next();
};
  1. We create the featureflow client
let featureflowExpress = new Featureflow.ExpressClient(config);
  1. We create a /features rest endpoint and use the featureflow.evaluateAll method to evaluate all features and pass them back as an object
app.get('/api/features', (req, res) => {
  return res.send(req.featureflow.evaluateAll(req.ffUser));
});
  1. On the client side (App.js) using react we call the rest endpoint in componentWillMount, set the values in the state and display
this.callApi()
      .then(res => this.setState({ features: res }))
      .catch(err => console.log(err));
  }
 <p>{
    Object.keys(this.state.features).map(key =>
        (<p>{key} : {this.state.features[key]}</p>)
    )
}</p>

For example, in an SPA you may call /features once for the anonymous user then again when a user logs in.

Further Reading

See http://docs.featureflow.io for more information

Additional Usage Details

Running the production build on localhost. This will create a production build, then Node will serve the app on http://localhost:5000

NODE_ENV=production yarn dev:server

How this works

The key to use an Express backend with a project created with create-react-app is on using a proxy. We have a proxy entry in client/package.json

"proxy": "http://localhost:5000/"

This tells Webpack development server to proxy our API requests to our API server, given that our Express server is running on localhost:5000

Credit

This example is based on an initial example from the devloper below, please consider passing credit to him.

Buy Me A Coffee

-Esau

featureflow-example-nodejs-client-server's People

Contributors

oliveroldfield avatar dependabot[bot] avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.