Skip to content

Commit

Permalink
Add path alias for url in route options
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Apr 12, 2017
1 parent 51e65f0 commit 4ae6067
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
20 changes: 10 additions & 10 deletions docs/Routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ fastify.route(options)
```
* `method`: currently it supports `'DELETE'`, `'GET'`, `'HEAD'`, `'PATCH'`, `'POST'`, `'PUT'` and `'OPTIONS'`.

* `url`: the path of the url to match this route.
* `schema`: an object containing the schemas for the request and response.
* `url`: the path of the url to match this route (alias: `path`).
* `schema`: an object containing the schemas for the request and response.
They need to be in
[JSON Schema](http://json-schema.org/) format, check [here](https://github.com/fastify/fastify/blob/master/docs/Validation-And-Serialize.md) for more info.

Expand Down Expand Up @@ -60,11 +60,11 @@ fastify.route({

<a name="shorthand-declaration"></a>
### Shorthand declaration
The above route declaration is more *Hapi*-like, but if you prefer an *Express/Restify* approach, we support it as well:
`fastify.get(path, [schema], handler)`
`fastify.head(path, [schema], handler)`
`fastify.post(path, [schema], handler)`
`fastify.put(path, [schema], handler)`
`fastify.delete(path, [schema], handler)`
`fastify.options(path, [schema], handler)`
`fastify.patch(path, [schema], handler)`
The above route declaration is more *Hapi*-like, but if you prefer an *Express/Restify* approach, we support it as well:
`fastify.get(path, [schema], handler)`
`fastify.head(path, [schema], handler)`
`fastify.post(path, [schema], handler)`
`fastify.put(path, [schema], handler)`
`fastify.delete(path, [schema], handler)`
`fastify.options(path, [schema], handler)`
`fastify.patch(path, [schema], handler)`
11 changes: 6 additions & 5 deletions fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,17 @@ function build (options) {
opts.contentTypeParser = opts.contentTypeParser || this._contentTypeParser
opts.hooks = opts.hooks || this._hooks

if (map.has(opts.url)) {
if (map.get(opts.url)[opts.method]) {
const url = opts.url || opts.path
if (map.has(url)) {
if (map.get(url)[opts.method]) {
throw new Error(`${opts.method} already set for ${opts.url}`)
}

map.get(opts.url)[opts.method] = opts
map.get(url)[opts.method] = opts
} else {
const node = buildNode(opts.url, router)
const node = buildNode(url, router)
node[opts.method] = opts
map.set(opts.url, node)
map.set(url, node)
}

// chainable api
Expand Down
27 changes: 27 additions & 0 deletions test/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,30 @@ fastify.listen(0, function (err) {
})
})
})

test('path can be specified in place of uri', t => {
t.plan(3)

fastify.listen(0, function (err) {
if (err) t.error(err)
fastify.server.unref()

fastify.route({
method: 'GET',
path: '/path',
handler: function (req, reply) {
reply.send({ hello: 'world' })
}
})

const reqOpts = {
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/path'
}
request(reqOpts, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEqual(JSON.parse(body), { hello: 'world' })
})
})
})

0 comments on commit 4ae6067

Please sign in to comment.