.api()
Source:
Access:
public

This is the main api class. With this class you can add/manage api routes, manage policies and change the api folders/urls. The api is used to easily obtain data from the server while also respecting things like security by using policies. The response for the api works in a RESTful manner and can be managed using the assigned JSON class object.

Summary

Name
Description
add
Creates a new api route.
Returns the folders class.
Loads an api or policy file.
Returns the policies class.
Creates a new policy.
Removes the specified api route.
Returns the urls class.

Functions

.add(url, policies)
Source:
Access:
public
Summary:
Creates a new api route.

This function is used to create a new api route. An api is a way to easily obtain data from you server that you need from your client. Apis can also be used to trigger actions on the server. This can be things like logging users in. For more information about api you can read the tutorial about it. You can set the url of the api with the url parameter. You can also add policies to your api route. Policies are functions that check if the client is allowed to continue on to the api function. You can manage policies using policies using the policy class and there also is a tutorial about policies.

Parameters

Name
Type
Description
url
String
The url the new api will route to.
policies
param
The policies for this api route. (optional) (default: [])

Examples

cerus.api().add("test")
.then(function(req, res) {
  res.emit();
});
// -> routes the api route to "/api/test"

Emits

Event
Description
request
When the api route has been requested. With the request class as first parameter, the json class as second parameter and the cerus object as last parameter.

Returns

Type
Promise
Description
This function will return a promise.
.folders()
Source:
Access:
public
Summary:
Returns the folders class.

This function will return the folders class for this module. With this class you can change the folders which are used to store the api classes and policy classes.

Returns

Type
Class
Description
The api.folders class.
.load(name, type)
Source:
Access:
public
Summary:
Loads an api or policy file.

With this function you can load an api or policy class. The api classes should contain the functions used for routing api requests. The policy classes should contain the policies. To load the class it will use the general require function. The folders that should contain the classes can be changed using the api.folders function.

Parameters

Name
Type
Description
name
String
The name of the class to load.
type
String
The type of class to load.

Returns

Type
Class
Description
This function will return the loaded class.
.policies()
Source:
Access:
public
Summary:
Returns the policies class.

With the class this function returns you can manage the policies for the api. The policies are used as middleware before the api function is called. These policies are used for things like security and to check if the client is logged in. You can easily add policies using the api.policy function.

Returns

Type
Class
Description
The policies class.
.policy(name)
Source:
Access:
public
Summary:
Creates a new policy.

This function is used for a shortcut to add a new policy. You can specify the name with the inserted parameter. This function will return a promise that will be called on the 'policy' event when the policy is used. The api request will be stopped when the policy has emitted a response. The 'policy' event will e called with the request, json response and cerus as parameters.

Parameters

Name
Type
Description
name
String
The name of the new policy.

Returns

Type
Promise
Description
This function will return a promise.
.remove(url)
Source:
Access:
public
Summary:
Removes the specified api route.

With this function you can remove an api route. It basically directly removes it from the router.

Parameters

Name
Type
Description
url
String
The url of the api route to remove.

Examples

cerus.api().remove("/");
// -> this removes the "/" api route
.urls()
Source:
Access:
public
Summary:
Returns the urls class.

This function will return the urls class for this module. This class is used to store the url for the api.

Returns

Type
Class
Description
The api.urls class.