Cookie

  • 通过 request 对象获取:







 



import { Route, Controller, Http } from '@dazejs/framework'

@Route('users')
export default class User extends Controller {
    @Http.Get()
    index() {
      const key = 'username';
      console.log(this.request.cookieValue(key)) // this.request.cookie 的别名方法
    }
}
1
2
3
4
5
6
7
8
9
10
  • 使用注入:





 

 



import { Controller, Route, Http, CookieValue } from '@dazejs/framework'

@Route('users')
export default class User extends Controller {
    @Http.Get()
    @CookieValue('username')
    index(usernameCookie) {
      console.log(usernameCookie)
    }
}
1
2
3
4
5
6
7
8
9
10

cookie 是跟随响应一起发送的,我们我们需要将 Cookie 实例附加在响应对象上来设置 cookie







 
 
 
 



import { Controller, Route, Http, Cookie } from '@dazejs/framework'

@Route('users')
export default class User extends Controller {
    @Http.Get()
    index() {
      const cookie = new Cookie('username', 'Dazejs')
      return this.response().withCookie(cookie).success({
        data: 'im data'
      })
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

response 对象上还添加了简便方法 cookie(name, value [, options]) 来设置 cookie







 
 
 



import { Controller, Route, Http, Cookie } from '@dazejs/framework'

@Route('users')
export default class User extends Controller {
    @Http.Get()
    index() {
      return this.response().cookie('username', 'Dazejs').OK({
        data: 'im data'
      })
    }
}
1
2
3
4
5
6
7
8
9
10
11

框架提供了 cookie 的默认配置,配置文件位于 src/config/cookie.js,默认使用该配置文件的配置来设置 cookie:

也可以使用自定义配置作为参数传入构造函数:







 
 
 






import { Controller, Route, Http, Cookie } from '@dazejs/framework'

@Route('users')
export default class User extends Controller {
    @Http.Get()
    index() {
      const cookie = new Cookie('username', 'Dazejs', {
        // options
      })
      return this.response().withCookie(cookie).OK({
        data: 'im data'
      })
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

TIP

单独设置 signed 配置后,获取 cookie 的时候需要传入一致的 signed 配置: this.request.cookieValue('username', { signed: false })