Controller配置指南

SugarServer中路由都是通过装饰的方式配置的。

例子

  1. import {
  2. Controller,
  3. router
  4. } from 'sugar-server';
  5. export class HelloWorldController extends Controller {
  6. @router.GetRoute('/')
  7. home () {
  8. return 'hello World!';
  9. }
  1. @router.GetRoute('/')

这行装饰器就完成路由配置,后面只要使用该controller就好了

支持的请求 method

  1. GetRoute 响应method=GET的请求
  2. PostRoute 响应method=POST的请求
  3. PutRoute 响应method=PUT的请求
  4. DelRoute 响应method=DELETE的请求
  5. AllRoute 响应所有method类型的请求

高阶使用

装饰器的良好特性,可以支持一个函数上同时使用多个装饰

  1. @router.GetRoute('/hello-word')
  2. @router.GetRoute('/hello-word-2')
  3. home () {
  4. return 'hello World!';
  5. }

这样不管你访问 /hello-word 还是 /hello-word-2都能看到 hello World!