Build SMI apps

Invoke the serverless component

From the app’s root directory,

  1. Navigate to the app.js file.
  2. Define the JSON payload that is to be passed to the method in the server.js file. In the sample app.js code displayed in the right pane, options is the JSON payload that is passed.
  3. Include the client.request.invoke(“serverMethodName”, JSON_payload) method to invoke the serverless component. By default, the serverless environment adds an iparams object to the payload.
  4. Include functions to handle the response obtained from the serverless component.

Send response to the front-end component

After the app logic in the server method runs, the server method sends an appropriate response to the front-end component. To enable this:

  1. Navigate to the server.js file. In the exports code block, define the server method that is called from the front-end component. Place the app logic inside the server method.

    Notes:
    • The SMI function name is case-sensitive.
    • The SMI function name:
      • Can be alphanumeric ([a-z], [A-Z], [0-9]) and can contain the special character underscore ( _ ).
      • Should not start with a number.
      • Should not contain any spaces between the characters.
      • Can be 2 to 40 characters long.
  2. Use the renderData(error, data) method to return a response to the front-end component.

    Notes:
    • error and data are passed as JSON objects to the caller method. Ensure that the first argument of renderData(error, data) is always the error object. For success responses, ensure to pass null as the first argument.

    • error is an object with status and message attributes. data is an object with valid <key>: <value> pairs.

    • The serverless environment adds a requestID attribute, to the error or data object.

  3. To send a success response, use the renderData() method as follows:

    exports = {
     serverMethod:  function(options) {
       // app logic resides here
       renderData(null,  { "key": "value" });
     }
    }
  4. To send a failure response, use the renderData() method as follows:

    exports = {
     serverMethod:  function(options) {
       var error = { status: 403, message: "Error while processing the request" };
       renderData(error);
     }
    }

    In the response if error.status is not present, the default status code of 500 is used as the error status. If the error object in renderData(error) is of incorrect JSON format, error.status value is 400 and error.message value is The error should be a JSON Object with a message or a status parameter.

    The front-end component can render this error as follows:

     {
       "requestID": "2edc13f8-3b81-4ade-b857-8d8e316fa87c",
       "status": 400,
       "message": "The error should be a JSON Object with a message or a status parameter."
     }

    Note:If the server method does not return a response, the app execution timeout error occurs.