Working with routers

In Foxx routers are used to definethe URLs of your API. The easiest way to use a router is to mount itdirectly in the service using the context:

  1. const createRouter = require("@arangodb/foxx/router");
  2. const router = createRouter();
  3. module.context.use(router);

Nested routers

Instead of mounting routers where they are defined, routers can also beexported from one module and imported in another. This allows you tostructure your routes by splitting them across multiple files:

  1. // in your main file
  2. const usersRouter = require("./api/users");
  3. module.context.use("/users", usersRouter);
  4. // in api/users/index.js
  5. const createRouter = require("@arangodb/foxx/router");
  6. const usersRouter = createRouter();
  7. module.exports = usersRouter;
  8. usersRouter.get("/me", (req, res) => {
  9. // this will be exposed as /users/me
  10. });

You can also mount routers inside of each other:

  1. // in api/users/friends.js
  2. const createRouter = require("@arangodb/foxx/router");
  3. const friendsRouter = createRouter();
  4. module.exports = friendsRouter;
  5. // in api/users/index.js
  6. const friendsRouter = require("./friends");
  7. usersRouter.use("/friends", friendsRouter);

Note that you can also mount several routers with the same prefixor even without a prefix:

  1. adminRouter.use(usersAdminRouter);
  2. adminRouter.use(groupsAdminRouter);

Local middleware

Router-level middleware only applies to the router it is applied to andis not shared between multiple routers mounted at the same prefix(or without a prefix).

This can be especially useful when restricting access tosome routes but not others:

  1. const createRouter = require("@arangodb/foxx/router");
  2. const publicRoutes = createRouter();
  3. const authedRoutes = createRouter();
  4. authedRoutes.use((req, res, next) => {
  5. if (req.session.uid) {
  6. next();
  7. } else {
  8. res.throw("unauthorized");
  9. }
  10. });
  11. module.context.use(publicRoutes);
  12. module.context.use(authedRoutes);

Router factories

Sometimes you may have routers you want to use in multiple projects oruse at multiple points of your API but with slightly different implementationsor using different collections.

In these cases it can be useful to return the router from a function thattakes these differences as arguments instead of exporting the router directly:

  1. // in your main file
  2. const createUsersRouter = require("../util/users-router");
  3. const usersRouter = createUsersRouter(
  4. module.context.collection("users"),
  5. "username"
  6. );
  7. module.context.use(usersRouter);
  8. // in util/users-router.js
  9. const createRouter = require("@arangodb/foxx/router");
  10. const { query } = require("@arangodb");
  11. module.export = (usersCollection, keyField) => {
  12. const router = createRouter();
  13. router.use((req, res) => {
  14. if (!req.session || !req.session.uid) {
  15. res.throw("unauthorized");
  16. }
  17. });
  18. router.get("/me", (req, res) => {
  19. const user = query`
  20. FOR user IN ${usersCollection}
  21. FILTER user[${keyField}] == ${req.session.uid}
  22. LIMIT 1
  23. RETURN user
  24. `.next();
  25. res.json(user);
  26. });
  27. return router;
  28. };