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的更多相关文章

  1. js中State模式的解析及运用

     状态模式,在大的范畴中的定义为当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类.每种编程语言有不同的实现方式,运用的范围也多用于游戏之中. 这里我用javascript来模拟状 ...

  2. [Hapi.js] View engines

    View engines, or template engines, allow you to maintain a clean separation between your presentatio ...

  3. [Hapi.js] Up and running

    hapi is a rock solid server framework for Node.js. Its focus on modularity and configuration-over-co ...

  4. [Angular 2] Managing State in RxJS with StartWith and Scan

    The scan operator in RxJS is the main key to managing values and states in your stream. Scan behaves ...

  5. [Hapi.js] Request Validation with Joi

    hapi supports request validation out of the box using the joi module. Request path parameters, paylo ...

  6. [Hapi.js] Friendly error pages with extension events

    hapi automatically responds with JSON for any error passed to a route's reply()method. But what if y ...

  7. [Hapi.js] Extending the request with lifecycle events

    Instead of using middlware, hapi provides a number of points during the lifecycle of a request that ...

  8. [Hapi.js] POST and PUT request payloads

    hapi makes handling POST and PUT payloads easy by buffering and parsing them automatically without r ...

  9. [Hapi.js] Serving static files

    hapi does not support serving static files out of the box. Instead it relies on a module called Iner ...

随机推荐

  1. android studio 开发android app 真机调试

    大家都知道开发android app 的时候可以有2种调试方式, 一种是Android Virtual Device(虚拟模拟器) ,另一种就是真机调试. 这里要说的是真机调试的一些安装步骤: 1. ...

  2. 【精品】Android游戏类源码大集合

    Android自定义效果——随机抽奖 一个模拟抽奖的效果,用户设定若干个选项,添加之后,就可以通过程序,来帮助随机选择其中一项出来.这个类似超市里面那种指针转盘抽奖,run之后是一个动态效果图,初始快 ...

  3. Hacker(19)----检测Windows系统漏洞

    想完全掌握Windows中存在的漏洞需要使用专业的漏洞扫描软件.目前常用的有MBSA(MircosoftBaselineSecurityAnalyzer).360安全卫士等. 一.使用MBSA检测系统 ...

  4. mysql三种binlog日志的理解

    最近,一直在纠结要不要改数据库binlog的日志格式,原先用的是row格式,导致数据库binlog日志较大,磁盘空间本来也不是很大,所以就想看看能不能改变binlog日志.在该binlog日志之前,先 ...

  5. IIS注册asp.net 4.0

    如果你是先装的VS后添加的IIS功能,那么你需要在ISS中注册NET Framework: 32位的Windows:------------------------------------------ ...

  6. Docker的简单认知

    Docker images: docker image是一个只读打模板,用来创建Docker 容器 Docker Registers 互联网上存储images的地方 Docker containers ...

  7. SQL Server链接MySQL实践

    最近在访问多数据库的时候进行了SQLServer链接MySQL数据的实践,现总结如下: 一.  安装mysql-connector-odbc驱动: 1. 在SQL Server服务器的机器上安装mys ...

  8. Css控制div水平垂直居中显示

    <style>#info{height:0px; width:0px;top:50%; left:50%;position:absolute;}#center{background:#FF ...

  9. silverlight 双坐标轴

    public void CreateLine(Grid oGrid, string sTitle, string sTableName, bool ifGetSig, string sYUint, s ...

  10. Unable to open log device '/dev/log/main': No such file or directory

    在我们使用真机进行Android应用调试时,无法获得调试信息,错误提示如下:Unable to open log device '/dev/log/main': No such file or dir ...