NodeJs

From DreamFactory
Jump to: navigation, search
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
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 [http://nodejs.org here] for more information.
+
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 [http://nodejs.org 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 ==
 
== Requirements ==
Line 10: Line 14:
  
 
DreamFactory passes in two [[DreamFactory/Features/Scripting#Resources Available To A Script|additional objects]] for use in the scripts.
 
DreamFactory passes in two [[DreamFactory/Features/Scripting#Resources Available To A Script|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.
+
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.'''
+
Using the '''platform''' resource...
 
<source lang="javascript">
 
<source lang="javascript">
 
var key = platform.session.api_key;
 
var key = platform.session.api_key;
Line 21: Line 25:
 
// Stop execution if verbs other than GET are used in Custom Scripting Service
 
// Stop execution if verbs other than GET are used in Custom Scripting Service
 
if (event.request.method !== "GET") {
 
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.
+
     throw new Error("Only HTTP GET is allowed on this endpoint."); // will result in a 500 back to client with the given message.
 
}
 
}
  
Line 34: Line 38:
 
event.response.content = {"test": "value"};
 
event.response.content = {"test": "value"};
 
</source>
 
</source>
 
  
 
== Including Other Scripts ==
 
== 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.  
+
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., <code> npm install -g <package_name></code> then you can require them in the script file or inside of scripting tab.
  
 
<source lang="javascript">
 
<source lang="javascript">
 
test = require('test.js');
 
test = require('test.js');
 
</source>
 
</source>
 +
 +
 +
'''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 :) )
 +
 +
[[File:Hellphp.png|800px]]
 +
 +
== Logging to your log file ==
 +
 +
To <code>console.log()</code> you must open the .env file and set APP_LOG_LEVEL to debug like so:
 +
 +
<code>APP_LOG_LEVEL=debug</code>
 +
 +
After making changes to your .env file, run the following command within your instance's root directory:
 +
 +
<code>$ php artisan config:clear</code>

Latest revision as of 21:54, 12 December 2018

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