fastify
Advanced tools
Comparing version 0.22.0 to 0.23.0
@@ -23,2 +23,6 @@ <h1 align="center">Fastify</h1> | ||
<a name="route-prefixing-option"></a> | ||
### Route Prefixing option | ||
If you pass an option with the key `prefix` with a `string` value, Fastify will use it to prefix all the routes inside the register, for more info check [here](https://github.com/fastify/fastify/blob/master/docs/Routes.md#route-prefixing). | ||
<a name="error-handling"></a> | ||
@@ -25,0 +29,0 @@ #### Error handling |
@@ -73,1 +73,37 @@ <h1 align="center">Fastify</h1> | ||
`fastify.patch(path, [schema], handler)` | ||
<a name="route-prefixing"></a> | ||
### Route Prefixing | ||
Sometimes you need to maintain two or more different versions of the same api, a classic approach is to prefix all the routes with the api version number, `/v1/user` for example. | ||
Fastify offers you a fast and smart way to create different version of the same api without changing all the route names by hand, *route prefixing*. Let's see how it works: | ||
```js | ||
// server.js | ||
const fastify = require('fastify')() | ||
fastify.register(require('./routes/v1/users'), { prefix: '/v1' }) | ||
fastify.register(require('./routes/v2/users'), { prefix: '/v2' }) | ||
fastify.listen(3000) | ||
``` | ||
```js | ||
// routes/v1/users.js | ||
module.exports = function (fastify, opts, next) { | ||
fastify.get('/user', handler_v1) | ||
next() | ||
} | ||
``` | ||
```js | ||
// routes/v2/users.js | ||
module.exports = function (fastify, opts, next) { | ||
fastify.get('/user', handler_v2) | ||
next() | ||
} | ||
``` | ||
Fastify will not complain because your are using the same name for two different routes, because at compilation time it will handle the prefix automatically *(this also means that the performances will not be affected at all!)*. | ||
Now your clients will have access to the following routes: | ||
- `/v1/user` | ||
- `/v2/user` | ||
You can to this as many time as you want, it works also for nested `register` and routes parameter are supported as well. |
@@ -74,2 +74,3 @@ 'use strict' | ||
fastify.route = route | ||
fastify._RoutePrefix = new RoutePrefix() | ||
@@ -188,3 +189,3 @@ // expose logger instance | ||
function override (instance, fn) { | ||
function override (instance, fn, opts) { | ||
if (fn[Symbol.for('skip-override')]) { | ||
@@ -199,2 +200,3 @@ return instance | ||
instance._hooks = Hooks.buildHooks(instance._hooks) | ||
instance._RoutePrefix = buildRoutePrefix(instance._RoutePrefix, opts) | ||
@@ -204,2 +206,16 @@ return instance | ||
function RoutePrefix () { | ||
this.prefix = '' | ||
} | ||
function buildRoutePrefix (r, opts) { | ||
function _RoutePrefix () {} | ||
const R = new _RoutePrefix() | ||
R.prefix = r.prefix | ||
if (typeof opts.prefix === 'string') { | ||
R.prefix += opts.prefix | ||
} | ||
return R | ||
} | ||
function close (cb) { | ||
@@ -270,3 +286,4 @@ runHooks( | ||
contentTypeParser: self._contentTypeParser, | ||
hooks: self._hooks | ||
hooks: self._hooks, | ||
RoutePrefix: self._RoutePrefix | ||
}) | ||
@@ -291,4 +308,6 @@ } | ||
opts.hooks = opts.hooks || this._hooks | ||
opts.RoutePrefix = opts.RoutePrefix || this._RoutePrefix | ||
const url = opts.url || opts.path | ||
const prefix = opts.RoutePrefix.prefix | ||
const url = prefix + (opts.url || opts.path) | ||
@@ -295,0 +314,0 @@ if (map.has(url)) { |
{ | ||
"name": "fastify", | ||
"version": "0.22.0", | ||
"version": "0.23.0", | ||
"description": "Fast and low overhead web framework, for Node.js", | ||
@@ -70,3 +70,3 @@ "main": "fastify.js", | ||
"ajv": "^5.1.5", | ||
"avvio": "^0.6.1", | ||
"avvio": "^1.0.0", | ||
"fast-json-stringify": "^0.12.1", | ||
@@ -73,0 +73,0 @@ "fast-safe-stringify": "^1.2.0", |
185831
84
5050
+ Addedavvio@1.0.0(transitive)
- Removedavvio@0.6.1(transitive)
Updatedavvio@^1.0.0