Skip to main content

REST APIs

Express is the most popular node web framework, which also serves as an underlying library for other popular node web frameworks. It offers mechanisms for:

  • Write handlers for requests using various HTTP verbs at different URL paths/routes.
  • Establish standard web application settings, such as the port to use for connection and the location of templates used for response rendering.
  • Add additional request processing middleware at any point within the request handling pipeline.

The koiiNode.js under the _koiNode directory provides an app object for creating the REST APIs. Its usage can be demonstrated as below.

Example:

app.post("/accept-cid", async (req, res) => {
try {
const cid = req.body.cid;
if (cid) {
console.log("CID =" + cid);
res.status(200).json({ message: "CID" });
}
} catch (err) {
console.log("CATCH IN ACCEPT CID", err);
}
});

Express endpoints can be defined in the index.js file of your task-template

// import app from koiiNode.js file
const { app, namespaceWrapper } = require('@_koii/namespace-wrapper');

async function setup() {
console.log("IN SETUP");
await namespaceWrapper.defaultTaskSetup();
}

async function execute() {
{
/*EXECUTE FUNCTION GOES HERE*/
}
}

setup().then(execute);

if (app) {
app.post("/accept-cid", doSomething());
}