{ DreamFactory: 'Blog' }

Server-side scripting with Node.js and DreamFactory

Posted by Todd Appleton

Thu, May 5, 2016

nodejs-banner.png

DreamFactory supports V8js, Node.js, PHP, and Python for server-side scripting. In a previous blog post I showed how to create a custom scripting service using V8js. Below, I'll show you the steps to enable and use Node.js scripting in your DreamFactory environment. It's recommended that you use DreamFactory 2.1.2 (or greater) where Node.js is fully supported. We also have a detailed wiki tutorial you can use as a technical reference.

The first step is obviously to make sure Node.js is installed on your system. In my case I'm on OS X Yosemite so I can do a 'which node' and see that the path to Node.js is /usr/local/bin/node.  'node -v' shows that I have v4.4.2. If you don't get a valid path go to https://nodejs.org to for instructions on downloading and installing Node.js for your system.

Once you know the path to the node executable you need to set it in your DreamFactory .env config file. This file is found under the main dreamfactory directory. For example, in a Bitnami setup it's htdocs/.env. If you installed from GitHub it would normally be dreamfactory/.env. Edit this file and set DF_NODEJS_PATH to point to your node location. Make sure you delete the ## to uncomment this line.

DF_NODEJS_PATH=/usr/local/bin/node

You may need to add quotes around the path, especially for Windows. This allows for spaces in the path.

DF_NODEJS_PATH="c:\Program Files\nodejs\node.exe"

To force a reload of config you can use the following command. Run it from the same directory as the .env file.

php artisan config:clear

Now you're ready to start writing your Node.js scripts. Next we'll create the same math service as the previous blog post but this time using Node.js.  The script is functionally the same as the V8js version. The only difference is how it loads the lodash library. For V8js there's a file lodash.min.js on the local file system which is loaded with require('lodash.min.js'). For Node.js lodash is installed using npm then loaded with require('lodash').

0.png

  • Go to the Services tab in the admin console.
  • Click Create and set Service Type to ‘Custom Scripting Service’.
  • Set the name to ‘math’. This will be part of the URL when you call the service.
  • Set the label to ‘Math’.
  • Click the Config tab and set the Scripting Engine Type to ‘Node.js’.
  • Paste the following code into the Content text area, then click Create Service to save your new service.
var params, required, n1, n2, resource, result;

var lodash = require("lodash");

if (event.request.method !== "GET") {
    throw("Only HTTP GET is allowed on this service.");
}

// get query params from request
params = event.request.parameters;

// get resource, /math —> "", /math/add —> "add"
resource = event.resource;
if (resource !== "") {
    // check for required params
    required = ["n1","n2"];
    lodash._.each( required, function( element ) {
        if (!params.hasOwnProperty(element) || !params[element]) {
            throw( "Missing '" + element + "' in params\n" );
        }
    });
    n1 = Number(params.n1);
    n2 = Number(params.n2);
}

switch (resource) {
    case "":
        // /math means return all supported resources
        result = {"resource": ["add", "subtract", "multiply", "divide"]};
        break;
    case "add":
        result = {"result": n1 + n2};
        break;
    case "subtract":
        result = {"result": n1 - n2};
        break;
    case "multiply":
        result = {"result": n1 * n2};
        break;
    case "divide":
        if (!n2) {
            throw("Divide by zero error.");
        }
        result = {"result": n1 / n2};
        break;
    default:
        throw("Invalid or missing resource name.");
        break;
}

return result;

The script uses lodash, so before you can run it you need to use npm to install lodash. Your script can include any other node modules you need in this same manner.

For Bitnami installs it's best to run npm from the htdocs directory:

npm install lodash

You can use the -g option if you like:

npm install -g lodash

Now that lodash is installed you can call this service from the REST API. Using your REST client of choice, make the following GET request. Replace everything before /api/v2 with your instance base URL. You can use HTTP basic auth or pass an authenticated session token in the API call.

GET http://127.0.0.1:8080/api/v2/math

For example, with cURL:

curl -i -k -3 -X GET "http://127.0.0.1:8080/api/v2/math" 
-H "X-DreamFactory-Session-Token: your-session-token"

The URL ends with the service name, ‘math’. Since there is no resource specified like add or subtract, the scripting service will return a list of available resources.

{
    "resource": [
    	"add",
		"subtract",
		"multiply",
		"divide"
	]
}

To add two numbers, make the following request. Now the URL also includes the resource 'add'.

GET http://127.0.0.1:8080/api/v2/math/add?n1=3&n2=5

The script returns the sum of n1 and n2 in JSON format.

{
    "result": 8
}

If you want to create a Swagger service definition you can follow the steps from the previous blog post. If you need some examples of both event and custom scripting with Node.js check out the wiki Tutorials. These should help get you started.

DreamFactory 2.0 Node.js V8js Tutorial

Introducing Alex Bowen, DreamFactory’s New Community and Developer Relations Team Member

Posted by Alex Bowen

Tue, May 3, 2016

 

AAEAAQAAAAAAAAMYAAAAJGEzMzJmMTY1LWQ1OTItNGZjZS1iYmQ3LTgxMWE4NDViMTY1Ng.jpg

Hey DreamFactory folks, I’m Alex.

I’m the newest member on the Developer Relations team, and will also be involved in the community.

I’ve done Community Management and Developer Relations for a few different tech startups and software companies, so I’ve worked very closely with developers of pretty much every programming language, and more! I’ve also been very involved in the Maker movement and have been to my fair share of Maker Faires! I love IoT and how technology can solve the world's most pressing problems. Open Source is on the rise, and so is the developer community. Developers are a key part in growing the solutions created with open source. That’s why I am here. I am keen to inspire, nurture and find ways to succeed and change the world together!

Screen_Shot_2016-05-02_at_10.06.16_AM.png

I strongly believe that open source technology is the future of business and community. That is also why I am particularly excited to join DreamFactory Software. Their solutions are the perfect incubator for creating positive change and elevating business solutions. It’s a competitive edge that helps you get your work done in a transformational way and creates opportunities for your ideas to flourish. Let’s transform business as usual - together!

You can find me at events, hackathons and conferences around the world. I will also be managing our online community forum and our social media channels. Tweet to me anytime. Can’t wait to see you on our forums, social media and at an event!

DreamFactory 2.1.2 released, includes app packages

Posted by Ben Busse

Mon, May 2, 2016

df212.png

DreamFactory version 2.1.2 just shipped. The big news in this release is application packages. The packages features makes it easy to bundle and deploy your apps, services, scripts, schema, and user roles between different DreamFactory instances. We also added virtual foreign key joins between SQL databases and MongoDB. That means you can join data between a SQL database and MongoDB with a few configuration clicks :-)

What's new:

  • BIG NEWS: Redesigned packages feature, including new system/package API and artisan commands for import and export. Easily export apps, services, scripts, schema, and roles as a zip file and import it into any DreamFactory instance. 
  • BIG NEWS: Add a virtual foreign key to connect data in any SQL database to data in MongoDB. Then make a single API calls with the "related" parameter to fetch related data from SQL and MongoDB. This is a super convenient way to join data between SQL and MongoDB without writing server-side scripts.
  • Added Redis and Memcached config options to dreamfactory:setup command
  • Added Memcached config values in environment
  • Updated dreamfactory:import-pkg command to use new package
  • Updated initial setup to import any packages available
  • Now handling file stream output using StreamedResponse
  • Added artisan dreamfactory:config-hhvm command to create hhvm config file
  • Basic support for www-form-urlencoded payload in API requests
  • Added rewrite rule to allow basic auth on AWS cloud and VM.

NoSQL MongoDB DreamFactory API SQL

Modus Create is newest DreamFactory Velocity partner

Posted by Joshua Curry

Thu, Apr 21, 2016

modus.png

We're excited to announce the latest addition to our Velocity partner program, Modus Create. As a DreamFactory Velocity partner, the Modus development team employs DreamFactory Enterprise to automatically generate backend APIs that can are transportable between development and production phases. DreamFactory empowers Modus to offer a non-disruptive way of mobilizing enterprise data.

By using DreamFactory, we’ve been able to reduce the time to value in engagements by automating the creation of APIs for a wide range of legacy backends. This practice strategically positions us to be the go-to firm to best deliver entire app portfolios for our customers.

Patrick Sheridan, co-founder and CEO of Modus Create

DreamFactory CEO Bill Appleton is particularly impressed with their use of DreamFactory as a primary API infrastructure tool.

Our customers need a lot of help meeting their mobile initiatives and look to us to recommend partners. We are thrilled to have a development partner as experienced as Modus Create join our program to not only provide app delivery, but more importantly help our customers build an API infrastructure.

Check the full press release for more info.

 

 

Velocity Partnerships

Introducing Tracy Osborn, DreamFactory’s New Developer Relations Team Member

Posted by Tracy Osborn

Wed, Apr 13, 2016

25725935023_b1131ca87b_o-small.jpg
Tracy speaking at Fluent Conference 2016

Hi everyone, I’m Tracy!

I’m the new team member on the developer relations team here at DreamFactory. I’m super excited to be here!

A little bit of background on me — I’ve been involved with the Python and Django communities over the last few years and I also wrote Hello Web App, a book series teaching introductory web app development with Django. 

Over the last year, I’ve fallen in love with speaking at conferences, running workshops, and planning events, and I’m so excited to be able to use this experience with DreamFactory. I’m hopefully running my first DreamFactory meetup in June for the California Bay Area (stay tuned to our Meetup.com page!) and will be speaking at WebDevCon, Self Conference, Write the Docs, and PyCon US over the next month or so.

In particular, I’ve always been interested in working in open source and DreamFactory’s open source API platform is going to be the perfect introduction. I’ll be joining the DreamFactory team at OSCON this year, and I’m looking forward to getting to know the open source community and chat about the work that DreamFactory is doing.

Looking forward to being a part of the DreamFactory community and hope to meet you all at an event around the world soon!

DreamFactory

Weekly Digest