The APIConfigurator
class provides methods to handle requests to non static resources. An instance of this configurator can be found with the Configurator.prototype.api
property:
const server = new Configurator();
// *Getting the API configurator:
let api_configurator = server.api;
Note: see Configurator.METHODS
.
APIResource[] - An array containing all the API resources
Note: readonly, use the add() method to add a new resource.
Starts an advanced configurator chain for the last added resource
AdvancedAPIConfigurator - The advanced configurator of the last added route
Note: Must be called only right after a route has been added.
- Error - If it's not being called right after a route has been added
server.api
// *Adding some GET route:
.get(...)
// *The following advanced chain will configure the last GET route:
.advanced
// *Setting some advanced configurations:
.header(...)
// *After the done() gets called, the chain goes back to the outer configurator again:
.done()
// *Continuing with the APIConfigurator:
.put(...);
// *The following lines will throw an error,
// as the advanced chain must be available only right after a route been added:
server.api
.advanced;
Registers a middleware for the given route and HTTP methods
methods
string|string[] - One or more supported HTTP methodsroute
string - The server routemiddleware
function|function[] - A valid Expressjs middleware function
APIConfigurator - This same configurator (for method chaining)
- TypeError - If methods is not a string or an array of strings
- Error - If some HTTP method is not supported
server.api
.add(Configurator.METHODS.POST, '/some-route', someMiddlewareFunction)
.add('POST', '/some-other-route', someOtherMiddlewareFunction);
Registers a middleware for the given GET route
Note: same as add() with the HTTP method set to GET
.
route
string - The server routemiddleware
function|function[] - A valid Expressjs middleware function
APIConfigurator - This same configurator (for method chaining)
Registers a middleware for the given POST route
Note: same as add() with the HTTP method set to POST
.
route
string - The server routemiddleware
function|function[] - A valid Expressjs middleware function
APIConfigurator - This same configurator (for method chaining)
Registers a middleware for the given PUT route
Note: same as add() with the HTTP method set to PUT
.
route
string - The server routemiddleware
function|function[] - A valid Expressjs middleware function
APIConfigurator - This same configurator (for method chaining)
Registers a middleware for the given DELETE route
Note: same as add() with the HTTP method set to DELETE
.
route
string - The server routemiddleware
function|function[] - A valid Expressjs middleware function
APIConfigurator - This same configurator (for method chaining)
Registers a middleware for the given HEAD route
Note: same as add() with the HTTP method set to HEAD
.
route
string - The server routemiddleware
function|function[] - A valid Expressjs middleware function
APIConfigurator - This same configurator (for method chaining)
Registers a middleware for the given PATCH route
Note: same as add() with the HTTP method set to PATCH
.
route
string - The server routemiddleware
function|function[] - A valid Expressjs middleware function
APIConfigurator - This same configurator (for method chaining)
Registers a middleware for the given OPTIONS route
Note: same as add() with the HTTP method set to OPTIONS
.
route
string - The server routemiddleware
function|function[] - A valid Expressjs middleware function
APIConfigurator - This same configurator (for method chaining)
Registers a middleware for all supported HTTP methods
Note: same as add() with all the supported HTTP methods.
Note: This method is NOT equivalent to the Expressjs's app.all().
route
string - The server routemiddleware
function|function[] - A valid Expressjs middleware function
APIConfigurator - This same configurator (for method chaining)
Registers a middleware for GET, POST, PUT, DELETE, HEAD and PATCH routes
Note: same as add() with the HTTP method set to GET
, POST
, PUT
, DELETE
, HEAD
and PATCH
.
route
string - The server routemiddleware
function|function[] - A valid Expressjs middleware function
APIConfigurator - This same configurator (for method chaining)
Retrieves the main configurator
Configurator - The main configurator (for configurator chaining)
// *Main 'Configurator' object:
server
.port(...)
.api
// *Inner 'APIConfigurator' object:
.add(...)
.post(...)
.put(...)
.done() // Returns the chain back to the main configurator
// *Main 'Configurator' object:
.start();