Router

Router

Constructor

new Router(opts, app)

Parameters:
Name Type Description
opts Object

Router options.

app Application

Application object.

Source:

Methods

register(path, methods, middlewares, opts) → {Route}

Create and register a route.

Parameters:
Name Type Description
path String

url path

methods Array

Array of HTTP verbs

middlewares Array

-

opts Object

-

Source:

resources(name, prefix, middleware) → {Router}

restful router api

Parameters:
Name Type Description
name String

Router name

prefix String

url prefix

middleware function

middleware or controller

Since:
  • 1.0.0
Source:
Example
```js
app.resources('/posts', 'posts')
app.resources('posts', '/posts', 'posts')
app.resources('posts', '/posts', app.role.can('user'), app.controller.posts)
```

Examples:

```js
app.resources('/posts', 'posts')
```

yield router mapping

Method | Path            | Route Name     | Controller.Action
-------|-----------------|----------------|-----------------------------
GET    | /posts          | posts          | app.controller.posts.index
GET    | /posts/new      | new_post       | app.controller.posts.new
GET    | /posts/:id      | post           | app.controller.posts.show
GET    | /posts/:id/edit | edit_post      | app.controller.posts.edit
POST   | /posts          | posts          | app.controller.posts.create
PUT    | /posts/:id      | post           | app.controller.posts.update
DELETE | /posts/:id      | post           | app.controller.posts.destroy

app.router.url can generate url based on arguments
```js
app.router.url('posts')
=> /posts
app.router.url('post', { id: 1 })
=> /posts/1
app.router.url('new_post')
=> /posts/new
app.router.url('edit_post', { id: 1 })
=> /posts/1/edit
```

url(name, params) → {String}

Parameters:
Name Type Description
name String

Router name

params Object

more parameters

Since:
  • 1.0.0
Source:
Example
```js
router.url('edit_post', { id: 1, name: 'foo', page: 2 })
=> /posts/1/edit?name=foo&page=2
router.url('posts', { name: 'foo&1', page: 2 })
=> /posts?name=foo%261&page=2
```