1
0
Fork 0

Add example of creating a router

This commit is contained in:
Alan Plum 2016-06-15 18:24:40 +02:00
parent 33b40e3bf6
commit ecbe8262aa
No known key found for this signature in database
GPG Key ID: 8ED72A9A323B6EFD
1 changed files with 26 additions and 1 deletions

View File

@ -357,10 +357,35 @@ afterRemove(doc);
!SECTION Controllers vs routers
Foxx Controllers have been replaced with [Routers](Router/README.md). This is more than a cosmetic change as there are significant differences in behaviour:
Foxx Controllers have been replaced with [routers](Router/README.md). This is more than a cosmetic change as there are significant differences in behaviour:
Controllers were automatically mounted when the file defining them was executed. Routers need to be explicitly mounted using the `module.context.use` method. Routers can also be exported, imported and even nested. This makes it easier to split up complex routing trees across multiple files.
Old:
```js
'use strict';
const Foxx = require('org/arangodb/foxx');
const ctrl = new Foxx.Controller(applicationContext);
ctrl.get('/hello', function (req, res) {
// ...
});
```
New:
```js
'use strict';
const createRouter = require('org/arangodb/foxx/router');
const router = createRouter();
module.context.use(router);
router.get('/hello', function (req, res) {
// ...
});
```
Some general changes in behaviour that might trip you up:
* When specifying path parameters with schemas Foxx will now ignore the route if the schema does not match (i.e. `/hello/foxx` will no longer match `/hello/:num` if `num` specifies a schema that doesn't match the value `"foxx"`). With controllers this could previously result in users seeing a 400 (bad request) error when they should instead be served a 404 (not found) response.