Monday, August 14, 2017

Hello World - Kinvey Flex SDK

Hello Friends,

Recently I have started work in Node.js for one of our project's backend development, We are using kinvey for our backend and I'm responsible for all the backend architecture & development. So in this post I'm going to show you usage of kinvey's flex sdk to create simple node service and run it.

1 - Create package
  1. $ mkdir helloworld
  2. $ cd helloworld
  3. $ npm init


2 - Install kinvey flex sdk in you node package.
  1. $ npm install kinvey-flex-sdk --save


3 - To use kinvey flex module in your project add require as following.
  1. const sdk = require('kinvey-flex-sdk');
4 - Check IP address of your work station or server.
My macbook has 192.168.1.123

5 - Write flex function code

  1. // File : index.js
  2. const sdk = require('kinvey-flex-sdk');
  3. sdk.service({ host: '192.168.1.123', port: 8080 }, function (err, flex) {
  4.  
  5.     const flexFunctions = flex.functions;   // gets the FlexFunctions object from the service
  6.  
  7.     function helloWorldHandler(request, complete, modules) {
  8.             return complete({ "message": "Hello World" }).ok().done();
  9.     }
  10.     flexFunctions.register('helloWorld', helloWorldHandler);
  11. });


6 - Run service
  1. $ node index.js
  2. $ Service listening on 8080


7 - Send request to our flex function, execute following command to see response from our node service that is written using kinvey's flex sdk.

  1. $ curl -X POST -H "Content-Type: application/json"  -d '{}' "http://192.168.1.123:8080/_flexFunctions/helloWorld"

Above command will return following json response.
  1. {"request":{"method":"POST","headers":{},"username":"","userId":"","objectName":"","tempObjectStore":{},"body":{}},"response":{"status":0,"headers":{},"body":{"message":"Hello World"},"statusCode":200,"continue":false}}

I hope this post will help you to understand basic of kinvey flex sdk, later I will try to write some posts on advance concepts of flex sdk.

Thanks.