Logotype Sitevision Developer
Log in
Log in

Can I call routes in a RESTApp from a Script or a WebApp?

Yes, RestAppInvokerFactory is the starting point to make calls to a RESTApp without any outgoing requests.

Published: 2022-07-22  |  Updated: 2024-09-26

RespAppInvokerFactory make it possible to make call routes in a WebbApp from a Script module.

Step 1 - Get an instance of RestAppInvokerFactory

For WebApp/RESTApp - use import:

js
import restAppInvokerFactory from '@sitevision/api/server/RestAppInvokerFactory';

For Script module - use require:

js
const restAppInvokerFactory = require('RestAppInvokerFactory');

Step 2 - Make use of tRestAppInvokerFactory

js
// The RESTApp can be found by identifier, node or as in this case the path const restApp = restAppInvokerFactory.fromPath('/rest-api/MyRestApp'); // The route is called (get, post, put or delete) and can be called with or without input data const response = restApp.get('/routeName'); // response is a script object representing the response (properties: headers, statusCode, statusMessage, body) if (response.statusCode !== 200) { // Handle error and return } // The body of the response contains the actual data const body = response.body;