Node

We have a default Node 6.10(LTS) support on our platform. Custom version support also available on request.

Since backbench is a Faas integrated with APIGATEWAY we provide a static convention to map your functions to API endpoints. (we call em public functions). And this is how we define them -

exports.endpoint = function(arg1, arg2){
//your code here
}

arg1 is the request object for node. This containers information about the HTTP state. Goes as;

{
"method": "",
"path": "",
"body": {},
"headers": {},
"query": {}
}

arg2 is a nodejs callback and is optional. Callback will let you see the response on your APIs. (not console.log)

Note: callback function should have an argument which is JSON.parsed.

  • To log your application, use console.log() and logs will be generated on Logs section.

  • To check compilation error, click on bug button(integrated JSHint).

  • For runtime failure, see your bench's dashboard.

Restrictions

  • Module name cannot be func.js.

  • Callback function's argument should be JSON.parsed() to use.

Example

  1. API to display Hello World!

exports.endpoint = function(request, callback){
console.log("Logged at " + new Date())
callback(undefined, {response: "Hello World"})
}

2. Running a background process (without callback).

exports.endpoint = function(request){
//this program can be used to log IP address,
//for the user hitting this api.
console.log(req["x-forwarded-for"]);
}

3. Using ESv6

exports.endpoint = (req, cb) =>{
cb(null, {message: "Hello World"})
}

4. Using node async/await.

var request = require("request");
exports.endpoint = async function(req, cb){
var body = await request.get('https://api.github.com/repos/peeyushsrj');
cb(undefined, body)
}

More about callback function

callback(Error error, Object result);
  • error – is an optional parameter that you can use to provide results of the failed function execution. When a function succeeds, you can pass null as the first parameter.

  • result – is an optional parameter that you can use to provide the result of a successful function execution. The result provided must be JSON.parse() compatible. If an error is provided, this parameter is ignored.