3.3.1 • Published 6 years ago

plover-router v3.3.1

Weekly downloads
5
License
Apache-2.0
Repository
github
Last release
6 years ago

plover-router

NPM version

为Plover应用提供Restful形式API。

使用

config/routes.js配置路由规则。

module.exports = ({ resources, get, post, use }) => {

  get('/', 'home#index');

  get('/patients/:id', 'patients#show');

  post('/products', 'products#create');

  resources('photos');

  use('/admin', require('../lib/middleware/admin'));
};

resources

resources('photos');

按约定会生成以下7个路由规则:

HTTP VerbPathModule#ActionUsed For
GET/photosphotos#indexdisplay a list of all photos
GET/photos/newphotos#newreturn an HTML form for creating a new photo
POST/photosphotos#createcreate a new photo
GET/photos/:idphotos#showdisplay a specific photo
GET/photos/:id/editphotos#editreturn an HTML form for editing a photo
PATCH/PUT/photos/:idphotos#updateupdate a specific photo
DELETE/photos/:idphotos#deletedelete a specific photo

指定需要的action:

resources('photos', { only: ['index', 'new', 'create'] })

显示配置路由

get('/profile', { module: 'users', action: 'show' })

namespace

namespace('admin', () => {
  resources('articles');
});

Nested Resources

resources('magazines', () => {
  resources('ads');
});

会生成如下路由规则:

GET      /magazines/1/ads
GET      /magazines/1/ads/new
POST     /magazines/1/ads
GET      /magazines/1/ads/2
GET      /magazines/1/ads/2/edit
PATH/PUT /magazines/1/ads/2
DELETE   /magazines/1/ads/2

API

{
  get(match, to);
  post(match, to);
  put(match, to);
  patch(match, to);
  delete(match, to);

  resource(name, { only }, [block]);
  namespace(name, block);

  use(match, middleware);
}

match

使用path-to-regexp解析。

to

有两种格式指定action

get('/profile', 'users#edit');
get('/profile', { module: 'users', action: 'edit' });

第二种还可以指定更多参数:

get(match, { module: {String}, action: {String}, query: {Object} });
3.3.1

6 years ago

3.3.0

7 years ago

3.2.0

7 years ago

3.1.0

7 years ago

3.0.0

7 years ago

3.0.0-beta.1

7 years ago

1.4.0

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.1

7 years ago

1.1.0

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago