Node.js
    • PDF

    Node.js

    • PDF

    Article Summary

    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.

    compute-15-2-101.png

    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.

    1. Create the package.json file in JSON.
      {
        "name": "my-action",
        "main": "index.js",
        "dependencies" : {
          "left-pad" : "1.1.3"
        }
      }
      
    2. 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;
      
    3. 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.
    4. Use the ZIP file of Step 3 and create an action on the console.
      compute-15-2-102.png
    5. Run the action, and check the result on the console.
    Caution

    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.

    Caution

    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 returning Promise. 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 the resolve() function is run within the callback, and the action is successfully completed. If the reject() 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 between start and end as follows.
      compute-15-2-104.png

    Was this article helpful?

    What's Next
    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.