Foxx service context

The service context provides access to methods and attributes that are specificto a given service. In a Foxx service the context is generally available as themodule.context variable. Within a router’s request handler the request andresponse objects’ context attribute also provide access to the context of theservice the route was mounted in (which may be different from the one the routehandler was defined in).

Examples

  1. // in service /my-foxx-1
  2. const createRouter = require('@arangodb/foxx/router');
  3. const router = createRouter();
  4. // See the chapter on dependencies for more info on
  5. // how exports and dependencies work across services
  6. module.exports = {routes: router};
  7. router.get(function (req, res) {
  8. module.context.mount === '/my-foxx-1';
  9. req.context.mount === '/my-foxx-2';
  10. res.write('Hello from my-foxx-1');
  11. });
  12. // in service /my-foxx-2
  13. const createRouter = require('@arangodb/foxx/router');
  14. const router2 = createRouter();
  15. module.context.use(router2);
  16. router2.post(function (req, res) {
  17. module.context.mount === '/my-foxx-2';
  18. req.context.mount === '/my-foxx-2';
  19. res.write('Hello from my-foxx-2');
  20. });
  21. const router1 = module.context.dependencies.myFoxx1.routes;
  22. module.context.use(router1);

The service context specifies the following properties:

  • argv: any

Any arguments passed in if the current file was executed as ascript or queued job.

  • basePath: string

The file system path of the service, i.e. the folder in which the servicewas installed to by ArangoDB.

  • baseUrl: string

The base URL of the service, relative to the ArangoDB server,e.g. /_db/_system/my-foxx.

  • collectionPrefix: string

The prefix that will be used by collection and collectionName to derivethe names of service-specific collections. This is derived from theservice’s mount point, e.g. /my-foxx becomes my_foxx.

  • configuration: Object

Configuration options for the service.

  • dependencies: Object

Configured dependencies for the service.

  • isDevelopment: boolean

Indicates whether the service is running in development mode.

  • isProduction: boolean

The inverse of isDevelopment.

  • manifest: Object

The parsed manifest file of the service.

  • mount: string

The mount point of the service, e.g. /my-foxx.

apiDocumentation

module.context.apiDocumentation([options]): Function

DEPRECATED

Creates a request handler that serves the API documentation.

Note: This method has been deprecated in ArangoDB 3.1 and replaced withthe more straightforward createDocumentationRouter method providing thesame functionality.

Arguments

See createDocumentationRouter below.

Examples

  1. // Serve the API docs for the current service
  2. router.get('/docs/*', module.context.apiDocumentation());
  3. // Note that the path must end with a wildcard
  4. // and the route must use HTTP GET.

createDocumentationRouter

module.context.createDocumentationRouter([options]): Router

Creates a router that serves the API documentation.

Note: The router can be mounted like any other child router(see examples below).

Arguments

  • options: Object (optional)

An object with any of the following properties:

  • mount: string (Default: module.context.mount)

The mount path of the service to serve the documentation of.

  • indexFile: string (Default: "index.html")

File name of the HTML file serving the API documentation.

  • swaggerRoot: string (optional)

Full path of the folder containing the Swagger assets and the indexFile.Defaults to the Swagger assets used by the web interface.

  • before: Function (optional)

A function that will be executed before a request is handled.

If the function returns false the request will not be processed any further.

If the function returns an object, its attributes will be used to overridethe options for the current request.

Any other return value will be ignored.

If options is a function it will be used as the before option.

If options is a string it will be used as the swaggerRoot option.

Returns a Foxx router.

Examples

  1. // Serve the API docs for the current service
  2. router.use('/docs', module.context.createDocumentationRouter());
  3. // -- or --
  4. // Serve the API docs for the service the router is mounted in
  5. router.use('/docs', module.context.createDocumentationRouter(function (req) {
  6. return {mount: req.context.mount};
  7. }));
  8. // -- or --
  9. // Serve the API docs only for users authenticated with ArangoDB
  10. router.use('/docs', module.context.createDocumentationRouter(function (req, res) {
  11. if (req.suffix === 'swagger.json' && !req.arangoUser) {
  12. res.throw(401, 'Not authenticated');
  13. }
  14. }));

collection

module.context.collection(name): ArangoCollection | null

Passes the given name to collectionName, then looks up the collection withthe prefixed name.

Arguments

  • name: string

Unprefixed name of the service-specific collection.

Returns a collection or null if no collection with the prefixed name exists.

collectionName

module.context.collectionName(name): string

Prefixes the given name with the collectionPrefix for this service.

Arguments

  • name: string

Unprefixed name of the service-specific collection.

Returns the prefixed name.

Examples

  1. module.context.mount === '/my-foxx'
  2. module.context.collectionName('doodads') === 'my_foxx_doodads'

file

module.context.file(name, [encoding]): Buffer | string

Passes the given name to fileName, then loads the file with the resulting name.

Arguments

  • name: string

Name of the file to load, relative to the current service.

  • encoding: string (optional)

Encoding of the file, e.g. utf-8. If omitted the file will be loaded as araw buffer instead of a string.

Returns the file’s contents.

fileName

module.context.fileName(name): string

Resolves the given file name relative to the current service.

Arguments

  • name: string

Name of the file, relative to the current service.

Returns the absolute file path.

use

module.context.use([path], router): Endpoint

Mounts a given router on the service to expose the router’s routes on theservice’s mount point.

Arguments

  • path: string (Default: "/")

Path to mount the router at, relative to the service’s mount point.

  • router: Router | Middleware

A router or middleware to mount.

Returns an Endpoint for the given router or middleware.

Note: Mounting services at run time (e.g. within request handlers orqueued jobs) is not supported.