From DreamFactory
Jump to: navigation, search

Node.js is a very popular and powerful asynchronous event driven framework that uses Google's V8 engine to execute Javascript code. DreamFactory calls Node.js similar to using its command line scripting to execute event scripts, passing in and out the relative event resources. Note: While the scripts are written in Javascript, not all functionality available in browser-based Javascript is available in Node.js. Go here for more information.


Node.js is separate application that currently is not part of the DreamFactory standard distributions. It must be installed on the server manually. Once installed, the DreamFactory environment can be updated to point to the application for running scripts. In the .env file located in the DreamFactory install directory, update the following entry with the full install path.


Accessing Resources

DreamFactory passes in two additional objects for use in the scripts. In Node.js, these resources are represented as javascript objects and can be accessed as normal. See the examples below.

Using the platform resource...Note: The platform.api resource does not currently work for Node.js scripting.

var key = platform.session.api_key;

Using the event resource...

// Stop execution if verbs other than GET are used in Custom Scripting Service
if (event.request.method !== "GET") {
    throw "Only HTTP GET is allowed on this endpoint."; // will result in a 500 back to client with the given message.
// Stop execution and return a specific status code
if (event.resource !== "test") {
    event.response.status_code = 400;
    event.response.content = {"error": "Invalid resource requested."};
// defaults to 200 status code
event.response.content = {"test": "value"};

Including Other Scripts

Any script available to execution in Node.js (i.e. accessible via normal include paths) is able to be included in scripts executed by DreamFactory. This includes any libraries pulled in by npm.

test = require('test.js');