[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 身份验证?
如何使用 dropzone.js 和 hapi.js 上传图片