[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}`))
时间: 2024-11-18 21:29:34

[Hapi.js] Managing State with Cookies的相关文章

js中State模式的解析及运用

 状态模式,在大的范畴中的定义为当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类.每种编程语言有不同的实现方式,运用的范围也多用于游戏之中. 这里我用javascript来模拟状态模式,主要思想是通过事件监听的效果来控制一个主要对象的状态.那么何为事件监听的效果呢?在其他语言中,可以通过多个对象的协作来完成状态模式,而我在javascript中可以通过对DOM节点进行事件操控来传递消息,使主要对象接收消息(当然不同状态接收到的消息所产生的反应是不一样的).而在javascr

[Hapi.js] View engines

View engines, or template engines, allow you to maintain a clean separation between your presentation layer and the rest of your application. This post will demonstrate how to use the vision plugin with hapi to enable template support. index.js serve

[Hapi.js] Serving static files

hapi does not support serving static files out of the box. Instead it relies on a module called Inert. This lesson will cover serving static files using Inert's custom handlers. 'use strict' var Hapi = require( 'hapi' ); var Boom = require('boom'); v

[Hapi.js] Using the response object

When you use reply method: let resp = reply('hello world') It actually return an response object. By using response object, you can modiy the response's status code, header, cookie, type and so on... server.route({ method: 'GET', path: '/', handler:

[Hapi.js] Request Validation with Joi

hapi supports request validation out of the box using the joi module. Request path parameters, payloads, and querystring parameters can be validated with joi's simple, 'use strict' const Hapi = require('hapi') const Joi = require('joi') const server

[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 your application needs errors rendered in HTML? This lesson shows how to implement friendly HTML error messages using hapi extension events. const Hapi

[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 you can hook-in to provide additional functionality, called "extension events". This lesson will give you an introduction to using extension even

[Hapi.js] Route parameters

Routing is a fundamental aspect of any framework. In this lesson, you'll learn how to use path parameters in hapi's router. We'll also touch on how the router uses specificity to order routes internally. Router Param: http://localhost:8000/ztw // Wil

[Hapi.js] Replying to Requests

hapi's reply interface is one of it's most powerful features. It's smart enough to detect and serialize objects, buffers, promises and even streams. This post will demonstrate first hand how to use the reply method to send your data to the client, no