zoukankan      html  css  js  c++  java
  • [Hapi.js] Managing State with Cookies

    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}`))
  • 相关阅读:
    HDU 2544 (Djikstra)
    HDU 1237(表达式求值)
    HDU1690 (Floyd)
    《大道至简》读后感
    第6周总结
    第8周总结
    第7周总结
    第四周总结
    第5周总结
    java程序
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5229555.html
Copyright © 2011-2022 走看看