List policies: JavaScript example

Use the following steps to run the example.

  1. Create a file named FirstStepsGetExample.js and copy the following example code to the file.
    exports.getPolicies = function(hostNameAndPort, apiSecretKey) {
      return new Promise((resolve, reject) => {
        // Deep Security module
        const api = require("@trendmicro/deepsecurity");
    
        // Create the client
        const defaultClient = api.ApiClient.instance;
        defaultClient.basePath = "https:// " + hostNameAndPort + "/api";
        const defaultAuthentication = defaultClient.authentications["DefaultAuthentication"];
        defaultAuthentication.apiKey = apiSecretKey;
    
        // Allow connection that is 'secured' with self-signed certificate - for development only
        process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
    
        // Create a PoliciesApi object
        const policiesApi = new api.PoliciesApi();
    
        // List policies. Use version v1 of the API
        policiesApi.listPolicies("v1")
          .then(policies => {
            resolve(policies);
          })
          .catch(error => {
            reject(error);
          });
      });
    };
  2. Create another file named App.js and copy the following code to the file:
    const path = require("path");
    
    const FirstStepsGetExample = require(path.resolve(__dirname, "./FirstStepsGetExample.js"));
    
    FirstStepsGetExample.getPolicies("localhost:4119", "3:zNi5ag8xPGpfEMElV0GxAIpTs5Ji8BQoCtXaTAgKkVM=")
      .then( policies => {
        console.log(policies);
      })
      .catch(error => {
        console.log(error);
    });
  3. In the App.js file, change the URL and secret key parameters of the call to FirstStepsGetExample.getPolicies according to your environment.
  4. Open a Command Prompt (Windows) or terminal (Linux) and enter the following command:

    node App.js

  5. If you are seeing the error TypeError: Cannot read properties of undefined (reading 'text'), it means that the SDK cannot establish a connection between APIClient and the Deep Security Manager (DSM) host. Check that the DSM host name is correct.
    TypeError: Cannot read properties of undefined (reading 'text') 
    at \nodejs-DSM-sdk\src\ApiClient.js:471 
    at Object.onceWrapper (node:events:646:26) 
    at ClientRequest.emit (node:events:526:28) at TLSSocket.socketErrorListener (node:_http_client:442:9) 
    at TLSSocket.emit (node:events:526:28) 
    at emitErrorNT (node:internal/streams/destroy:157:8) 
    at emitErrorCloseNT (node:internal/streams/destroy:122:3) 
    at processTicksAndRejections (node:internal/process/task_queues:83:21)