NodeJs

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.

Note: These scripts are not sandboxed like V8js, so care must be taken to prevent unwanted access to system resources, e.g. the file system of the host server that is running DreamFactory.

Requirements

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.

 DF_NODEJS_PATH=/usr/local/bin/node

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...

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 new Error("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."};
    return;
}
 
// 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) can be included in scripts executed by DreamFactory. This includes any libraries pulled in by npm. You need to set the absolute path for any directories (node_modules, for instance) if you are not globally installing the dependencies with npm. You can ignore that if you are installing the dependencies globally inside the DreamFactory instance, i.e., npm install -g <package_name> then you can require them in the script file or inside of scripting tab.

test = require('test.js');


Note: When pulling in a script from a different folder or file service, you need to make sure the file service is specified in the roles that will be accessing the script service. In this case, the files are hosted in an S3 bucket, and the service is called hellophp(yes we are aware this is the Node page :) )

Hellphp.png

Logging to your log file

To console.log() you must open the .env file and set APP_LOG_LEVEL to debug like so:

APP_LOG_LEVEL=debug

After making changes to your .env file, run the following command within your instance's root directory:

$ php artisan config:clear