mirror of https://gitee.com/bigwinds/arangodb
Add example of creating a router
This commit is contained in:
parent
33b40e3bf6
commit
ecbe8262aa
|
@ -357,10 +357,35 @@ afterRemove(doc);
|
||||||
|
|
||||||
!SECTION Controllers vs routers
|
!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.
|
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:
|
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.
|
* 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.
|
||||||
|
|
Loading…
Reference in New Issue