This topic shows you how to perform operations by using SDK for Node.js to call the required API operations with efficiency. For example, you can create and invoke functions.

Prerequisites

Before you begin, make sure that you have completed the following operations:

Sample code of SDK for Node.js

The following code provides an example on how to create and invoke functions:

'use strict';

var FCClient = require('@alicloud/fc2');

var client = new FCClient('<account id>', {
  accessKeyID: '<access key id>',
  accessKeySecret: '<access key secret>',
  region: 'cn-shanghai',
  timeout: 10000 // The request timeout period. Unit: milliseconds. Default value: 10000.
});

var serviceName = '<service name>';
var funcName = '<function name>';

client.createService(serviceName).then(function(resp) {
  console.log('create service: %j', resp);
  return client.createFunction(serviceName, {
    functionName: funcName,
    handler: 'index.handler',
    memorySize: 128,
    runtime: 'nodejs4.4',
    code: {
      zipFile: fs.readFileSync('/tmp/index.zip', 'base64'),
    },
  });
}).then(function(resp) {
  console.log('create function: %j', resp);
  return client.invokeFunction(serviceName, funcName, 'event');
}).then(function(resp) {
  console.log('invoke function: %j', resp);
}).catch(function(err) {
  console.error(err);
});

Sample code of SDK for Node.js used for asynchronous invocation

Note
  • Asynchronous invocation is supported in Node.js 7.6 and later.
  • For more information about the request headers for asynchronous invocation, see PutFunctionAsyncInvokeConfig.

The following code provides an example on how to create and asynchronously invoke functions:

'use strict';

const FCClient = require('@alicloud/fc2');

var client = new FCClient('<account id>', {
  accessKeyID: '<access key id>',
  accessKeySecret: '<access key secret>',
  region: 'cn-shanghai',
});

var serviceName = '<service name>';
var funcName = '<function name>';

async function test () {
  try {
    var resp = await client.createService(serviceName);
    console.log('create service: %j', resp);

    resp = await client.createFunction(serviceName, {
      functionName: funcName,
      handler: 'counter.handler',
      memorySize: 128,
      runtime: 'nodejs4.4',
      initializer: 'counter.initializer',
      code: {
        zipFile: fs.readFileSync('/tmp/counter.zip', 'base64'),
      },
    });
    console.log('create function: %j', resp);

    // By default, responses are decoded in UTF-8.
    resp = await client.invokeFunction(serviceName, funcName, null);
    console.log('invoke function: %j', resp);

    // If the rawBuf parameter is set to true in options, respWithBuf is returned as the buffer.
    respWithBuf = await client.invokeFunction(serviceName, funcName, null, {}, 'LATEST', {rawBuf:true});

    uResp = await client.updateFunction(serviceName, funcName, {
      description: 'updated function desc',
      initializationTimeout: 60,
    });
    console.log('update function: %j', resp);
  } catch (err) {
    console.error(err);
  }
}
test().then();