Routers

    Routers let you define routes that extend ArangoDB’s HTTP API with custom endpoints.

    Routers need to be mounted using the use method of a to expose their HTTP routes at a service’s mount path.

    You can pass routers between services mounted in the same databaseas dependencies. You can even nest routerswithin each other.

    createRouter(): Router

    This returns a new, clean router object that has not yet been mounted in theservice and can be exported like any other object.

    router.get([path], […middleware], handler, [name]): Endpoint

    router.post([path], […middleware], handler, [name]): Endpoint

    router.put([path], […middleware], handler, [name]): Endpoint

    router.patch([path], […middleware], handler, [name]): Endpoint

    router.delete([path], […middleware], handler, [name]): Endpoint

    router.all([path], […middleware], handler, [name]): Endpoint

    Arguments

    • path: string (Default: "/")

    The path of the request handler relative to the base path the Router is mounted at.If omitted, the request handler will handle requests to the base path of the Router.For information on defining dynamic routes see the section on.

    Zero or more middleware functions that take the following arguments:

    • req: Request

    An incoming server request object.

    • res: Response

    An outgoing server response object.

    • next: Function

    A callback that passes control over to the next middleware functionand returns when that function has completed.

    If a truthy argument is passed, that argument will be thrown as an error.

    If there is no next middleware function, the handler will beinvoked instead (see below).

    • handler: Function

    A function that takes the following arguments:

    An incoming server request object.

    • res: Response

    An outgoing server response.

    • name: string (optional)

    Returns an Endpoint for the route.

    Examples

    Simple index route:

    Restricting access to authenticated ArangoDB users:

    Multiple middleware functions:

    router.use([path], middleware, [name]): Endpoint

    The use method lets you mount a child router or middleware at a given path.

    Arguments

    • path: string (optional)

    The path of the middleware relative to the base path the Router is mounted at.If omitted, the middleware will handle requests to the base path of the Router.For information on defining dynamic routes see the section on.

    • middleware: Router | Middleware

    An unmounted router object or a middleware.

    A name that can be used to generate URLs for endpoints of this router.For more information see the method of the .Has no effect if handler is a Middleware.