HTTP Add-On

Quick Start

Creating your first JSON Based HTTP Function:

const {LOG_LEVEL, CelastrinaError, Configuration} = require("@celastrina/core");
const {HTTPAddOn, JSONHTTPContext, JSONHTTPFunction} = require("@celastrina/http");

class MyFirstFunction extends JSONHTTPFunction {
constructor(config) {super(config);}

async _get(context) {
context.log("Welcome to Celastrina!", LOG_LEVEL.INFO, "MyFirstFunction ._get(context)");
context.send({name: "sample", message: }); // Return whatever object you'd like
context.done();
}
}

const _config = new Configuration("MyFirstFunction");
const _httpconfig = new HTTPAddOn();

_config.setAuthorizationOptimistic(true); // Allow anon access, Celastrina defaults to pessimistic AuthN/AuthZ
_config.addOn(_httpconfig);

module.exports = new MyFirstFunction (_config);

You will need to make a few updates to your function.json. You’ll need to add an “entryPoint” attribute with the value “execute” and insure your in/out bindings are named “req” and “res” respectively.

{ 
     "entryPoint": "execute", 
     "bindings": [ 
          { 
               "authLevel": "function", 
               "type": "httpTrigger", 
               "direction": "in", 
               "name": "req", 
               "methods": [ "get"]
          }, 
          { 
               "type": "http", 
               "direction": "out", 
               "name": "res" 
          } 
     ]
}

All HTTP methods are supported, including custom ones. Simple add an async function to your class using the following naming convention:

_{method name}(context) {} Where {method name} is the HTTP method. Example:

async _post(context) {}
async _put(context) {}

Getting local app settings is fairly straight forward:

async _get(context) { 
     context.log("Welcome to Celastrina!", LOG_LEVEL.INFO, "MyFirstFunction ._get(context)"); 

     // From the app function Confiuration settings instead of process.env.
     let _property = await context.properties.getProperty("celastrinajs.test");

     context.send({name: "sample", message: });  // Return whatever object you'd like
     context.done(); 
}

That’s it! This is the most basic form of function. At first glance, this may seem more complicated then the base example from Microsoft or frameworks like express.js. The real value in Celastrinajs comes from the integrations required to provide an end-to-end modern, n-factor, cloud application. To leverage security and other PaaS services within Azure, including JWT, Azure App Config, and Azure Key Vault, we recommend more advanced configurations. Please read the Framework Architecture below for the best practices of Celastrinajs.

Please visit the Architecture Framework section on our home page for more details.

Context

Session

Session Resolver