Updated on 2023-07-24 GMT+08:00

Node.js Template

Stateful function:
// funcName: nodejsstateful
// Node.js does not adapt to context.getState context.setState and can be accessed using context.state.
const { Function } = require("function-javascript-sdk");
 
module.exports.initState = function (event, context) {
    context.state = { counter: 0 };
};
 
module.exports.handler = async (event, context) => {
    context.state.counter++;
    var func = new Function(context);
    await func.saveState();
    return context.state;
}

Invoking function:

// funcName: nodejscaller
const { Function } = require("function-javascript-sdk");
 
module.exports.newStateRouter = async (event, context) => {
        func = new Function(context, "nodejsstateful", "test1");
        await func.init();
        instanceID = func.getInstanceID();
        return instanceID;
}
 
module.exports.bindStateRouter = async (event, context) => {
        func = new Function(context);
        // bind
        await func.getInstance("nodejsstateful", "test1");
        instanceID = func.getInstanceID();
        return instanceID;
}
 
module.exports.invoke = async (event, context) => {
        var func = new Function(context);
        // bind
        await func.getInstance("nodejsstateful", "test1");
        var obj = await func.invoke({});
        var result = await obj.get();
        return result;
}
 
module.exports.terminate = async (event, context) => {
    var func = new Function(context);
    // bind
    await func.getInstance("nodejsstateful", "test1");
    var obj = await func.terminate();
    var result = await obj.get();
    return result;
}