[Hapi.js] Managing State with Cookies

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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}`))

 

以上是关于[Hapi.js] Managing State with Cookies的主要内容,如果未能解决你的问题,请参考以下文章

如何在 hapi.js 上的路径上使用 hapi-auth-jwt2 身份验证?

hapi.js - 404 路由 VS 静态文件路由

如何使用 dropzone.js 和 hapi.js 上传图片

Hapi.js —— Node.js 服务器端应用开发框架

“502 Bad Gateway”将 hapi.js 部署到 AWS Beanstalk?

[Hapi.js] Replying to Requests