- Print
- PDF
Node.js
- Print
- PDF
Available in Classic and VPC
This document describes how to create and use Node.js actions in a variety of ways, and includes application examples.
Create actions
A code written in JavaScript may contain multiple functions, but the main function must be declared as the starting point of a program. With this taken into account, the following is the simple example code hello.js
in JavaScript, printing "Hello World," along with a name and location.
function main(params) {
var name = params.name || 'World';
var place = params.place || 'Naver';
return {payload: 'Hello, ' + name + ' in ' + place + '!'};
}
The following is the process of an action called "hello" in the console, using the code written above.
Create actions by packaging with Node.js module
When writing code, there are cases where multiple files need to be created, or a different library than the library provided by default needs to be used. In such cases, related files can be packaged in a module, and an action can be created using the packaged file. The following is a sequence for creating an action by packing with the Node.js module through a simple example.
- Create the
package.json
file in JSON.{ "name": "my-action", "main": "index.js", "dependencies" : { "left-pad" : "1.1.3" } }
- Create the simple "index.js" file that uses "left-pad".
function myAction(args) { const leftPad = require("left-pad") const lines = args.lines || []; return { padded: lines.map(l => leftPad(l, 30, ".")) } } exports.main = myAction;
- Install the "left-pad" library with the
npm install
command, and then create the ZIP file.$ npm install $ zip -r action.zip *
- When creating the ZIP file, the "package.json" file must be located under the root of the ZIP file.
- Use the ZIP file of Step 3 and create an action on the console.
- Run the action, and check the result on the console.
At the moment, the action is not executed normally if a binary file is included among the dependencies installed when running npm install
.
Create asynchronous actions
You can create actions that work asynchronously. Some JavaScript functions that work asynchronously need to return activation result values after the main
function is finished. In such cases, the solution is to return Promise
within the action. The following is example code creating the asynchronous action, asyncAction
.
Please note that running code like the example may hog resources and cause excessive fees.
function main(args) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve({ done: true });
}, 2000);
})
}
- The
main
function is returningPromise
. This means the action is not finished running, and it will be completed in the future. - A callback will run after 2 seconds by the
setTimeout
function. The promise is met at the point when theresolve()
function is run within the callback, and the action is successfully completed. If thereject()
function is called, then it indicates that the action is completed in an abnormal way. - If you check the execution result information after creating and running
asyncAction
in the console in the same way, then you can see that there is a difference of about 2 seconds in timestamps betweenstart
andend
as follows.