Manually Executing Server Code

You can also execute your server code directly at any time from the client side. When you directly execute server code, you can also pass some custom parameters.

Let's explore how you can manually execute server code. Let us assume that you've deployed the following server code.

function main(params, context, done) {
  var user = KiiUser.userWithUsername(params.username,
                                      params.password);
  user.register({
    success: function(user) {
      done(user.getUsername());
    },
    failure: function(user, errorString) {
      done(errorString);
    }
  });
}

This is the sample code we've presented in Writing Server Code. The sample code is expecting to receive a username and password from a client device as the custom parameter, and it will execute the sign-up process with these parameters (e.g. allowing an application user to sign up on behalf of his/her friend).

Note that the success/failure status within the server code doesn't match that on the client. In this sample code, the call from the client will be successful even if the process within the server code fails because the client will receive the error string successfully. The client will fail when it cannot call the server code. In order to send the error in the server code to the client, implement the server code to return the success/failure status of it as the return value.

Check the execution result

When the manual execution of a server code is done, a line containing servercode.invoke is recorded in the developer log.

2017-10-04T11:31:35.000+09:00 [DEBUG] servercode.invoke description:Invoked Server Code versionID:gulsdf6ful8jvf8uq6fe7vjy6 args:{"username":"name_of_my_friend","password":"password_for_my_friend"} endpoint:main auth-header: response-type:application/json response-step:7 environmentVersion:6

The following table summarizes some of the important information recorded in the developer log.

Item name Description
description The body of the message. A fixed string will be outputted when a server code is executed.
versionID The code indicating the version of the executed server code.
args The JSON string that has been set as a parameter when the server code was manually executed.
endpoint The name of the server code endpoint that was executed.
response-step The executed step count.
environmentVersion The number indicating the version of the JavaScript engine used for executing the server code.