# State Decorator operation input response koa

Module
import { State } from "@tsed/platform-koa"
Source/packages/platform-koa/src/decorators/state.ts

# Overview

function State(expression: string): ParameterDecorator;
export function State(): ParameterDecorator;
Param Type Description
expression string The path of the property to get.

# Description

State return the value from koa.ctx.state object.

# Example

@Middleware()
class StateMiddleware {
  use(@State() state: any) {
     // set some on locals
     state.user = "user"
  }
}

@Controller('/')
@UseBefore(StateMiddleware)
class MyCtrl {
   @Get('/')
   @Render('home.ejs') // will use locals and returned data to render the page
   get(@State('user') user: any) {
      console.log('user', user);

      return {
        description: 'Hello world'
      }
   }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Other topics