1

I'm currently trying to migrate my code from ArangoDB v2.8 to v3.0.1 and I'm stuck with routes. I have a simple route like:

const router = require('@arangodb/foxx/router')()

router.get('/hello', function (req, res) {
  res.json({ hi: 'world' })
})

and my BASE URL is:

and of course my router is registered in manifest.json as "main": "index.js",

but when I'm trying to access it via /_db/ilearn/api/hello I'm getting 404 "unknown path '/api/hello'".

I have tried changing URL in all possible ways, nothing helps. What am I doing wrong here?

Thanks

artnikpro
  • 5,487
  • 4
  • 38
  • 40

1 Answers1

1

Unlike controllers, routers are not mounted automatically. This allows passing them around as exports and nesting them arbitrarily. The "main" file also does not register routers (like the "controllers" files did for controllers) but merely specifies the entry point of your service.

In order to mount a router you need to use the module.context.use function. You can mount a router on the service's mount point directly by omitting the path and just passing the router: module.context.use(router).

If you haven't seen it, I would also recommend checking out the Migration Guide, which covers other "gotchas" you may encounter when migrating from 2.x to 3.0: https://docs.arangodb.com/3.0/Manual/Foxx/Migrating2x/index.html

This gotcha in particular is covered in the chapter on migrating controllers: https://docs.arangodb.com/3.0/Manual/Foxx/Migrating2x/Controllers/index.html

Alan Plum
  • 10,814
  • 4
  • 40
  • 57
  • 1
    You are right! I guess I just missed this `Routers need to be explicitly mounted using the module.context.use method.` from the migration guide. It would be great if this would be also placed in the example here https://docs.arangodb.com/3.0/Manual/Foxx/Migrating2x/Controllers/index.html . Thanks a ton! – artnikpro Jul 08 '16 at 12:26
  • @artnikpro It was in the original draft but our testers found it confusing because the migration guide starts out by replacing the controllers section in the manifest with a main file that requires and mounts the routers, so if you combined both examples you'd end up mounting them twice. – Alan Plum Jul 08 '16 at 18:07