码迷,mamicode.com
首页 > Windows程序 > 详细

[Hapi.js] Managing State with Cookies

时间:2016-03-01 06:18:59      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:

hapi has built-in support for parsing cookies from a request headers, and writing cookies to a response, making state management easy and straight-forward. It even has built in support for cookie encryption and auto detects when a cookie contains JSON, parsing or stringifying automatically.

 

‘use strict‘
const Hapi = require(‘hapi‘)
const server = new Hapi.Server()
server.connection({ port: 8000 })

// set default cookie
server.state(‘hello‘, {
  ttl: 60 * 60 * 1000,  // expiry time
  isHttpOnly: true,
  encoding: ‘iron‘,
  password: ‘a5LewP10pXNbWUdYQakUfVlk1jUVuLuUU6E1WEE302k‘
})

server.route({
  method: ‘GET‘,
  path: ‘/‘,
  config: {
    handler: function(request, reply) {
      // read cookie
      let hello = request.state.hello
      reply(`Cookies! ${hello}`)
        .state(‘hello‘, ‘world‘) // set cookie
    }
  }
})

server.start(() => console.log(`Started at: ${server.info.uri}`))

 

[Hapi.js] Managing State with Cookies

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5229555.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!