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 service context to expose their HTTP routes at a service’s mount path.

    You can pass routers between services mounted in the same database . You can even nest routers within each other.

    createRouter(): Router

    This returns a new, clean router object that has not yet been mounted in the service 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

    These methods let you specify routes on the router. The all method defines a route that will match any supported HTTP verb, the other methods define routes that only match the HTTP verb with the same name.

    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 path parameters in the chapter on router endpoints.

    • middleware: Function (optional)

      • 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 function and 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 be invoked instead (see below).

    • handler: Function

      A function that takes the following arguments:

      • req: Request

        An incoming server request object.

      • res: Response

        An outgoing server response.

    • name: (optional)

      A name that can be used to generate URLs for the endpoint. For more information see the reverse method of the .

    Examples

    Simple index route:

    Restricting access to authenticated ArangoDB users:

    1. router.get('/secrets', function (req, res, next) {
    2. if (req.arangoUser) {
    3. } else {
    4. res.throw(404, 'Secrets? What secrets?');
    5. }
    6. }, function (req, res) {
    7. res.download('allOurSecrets.zip');
    8. });

    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 path parameters in the chapter on router endpoints.

    • middleware: Router | Middleware

      An unmounted router object or a .

    • name: string (optional)

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

    Returns an for the middleware or child router.

    When mounting child routers at multiple paths, effects of methods invoked on each endpoint will only affect routes of that endpoint.

    Examples

    1. const child = createRouter();
    2. router.use("/a", child);
    3. router.use("/b", child)
    4. .queryParam("number", joi.number().required(), "Required number parameter.");
    5. child.get(function (req, res) {
    6. // The query parameter "number" is required if the request was made via "/b"
    7. // but optional if the request was made via "/a", we can't rely on it.
    8. res.json({number: req.queryParams.number});
    9. });

    Examples

    1. router.header(
    2. 'x-common-header',
    3. joi.string().required(),
    4. 'Common header shared by all routes on this router.'
    5. );
    6. router.get('/', function (req, res) {
    7. // This route requires the header to be set but we don't need to explicitly
    8. // specify that.
    9. const value = req.header('x-common-header');