Cookies 在hapi上进行服务器渲染时,如何请求cookie?

Cookies 在hapi上进行服务器渲染时,如何请求cookie?,cookies,fetch,server-side,hapijs,Cookies,Fetch,Server Side,Hapijs,我知道在使用express时,可以使用以下代码请求cookie: req.get('cookie') 但是,当使用hapi服务器请求cookie时,我现在面临一个问题 请求cookie的代码应如下所示: request.state['COOKIE_NAME'] 但是,当服务器呈现我的页面时,我的request.state总是emtpy。在客户端上请求cookie时,没有问题,request.state中填充cookie 在我的服务器文件上,我正在使用onPreResponse钩子,如下所示:

我知道在使用express时,可以使用以下代码请求cookie:

req.get('cookie')
但是,当使用hapi服务器请求cookie时,我现在面临一个问题

请求cookie的代码应如下所示:

request.state['COOKIE_NAME']
但是,当服务器呈现我的页面时,我的
request.state
总是emtpy。在客户端上请求cookie时,没有问题,
request.state
中填充cookie

在我的服务器文件上,我正在使用
onPreResponse
钩子,如下所示:

server.ext( 'onPreResponse', ( request, reply ) => {
...
  fetch('http://localhost:3000/api/getcookie', {
    credentials: 'same-origin',
    headers: {'Cookie': request.state['COOKIE_NAME']} // Still empty
  })
...
});
hapi路线为:

{
  method: 'GET',
  path: '/getcookie',
  config: {
    auth: false
},
  handler: ( request, reply ) => {
    console.log('GET COOKIE ROUTE: ', request.state); // <-- this is empty when server rendering

    reply({
      statusCode: 200,
      message: 'get cookie',
      data: {
        text: request.state
      }
    })
    .code(200);
}
{
方法:“GET”,
路径:'/getcookie',
配置:{
作者:错
},
处理者:(请求、回复)=>{

console.log('GET COOKIE ROUTE:',request.state);//你的问题对我来说有点难理解。你写道

cookie设置没有问题,我也可以在客户端检索它们。问题是当我尝试在服务器上获取cookie时

…但我没有看到任何代码实际设置cookie值。因此我无法理解您如何成功进入客户端。我也不清楚在服务器
onPreResponse
扩展点中使用某种
fetch
方法请求相同的路由

我编写了一个小而完整的示例,它实际上设置了一个cookie,还使用了
onPreResponse
扩展点

'use strict';

const Hapi = require('hapi');
const Path = require('path');

// Create a server with a host and port
const server = new Hapi.Server();
server.connection({
  port: 3000
});

//Define the cookie
server.state('data', {
    encoding: 'base64json'
});

// Add the route
server.route({
  method: 'GET',
  path: '/getcookie',
  handler: function(request, reply) {

    const counterState = request.state.data.counter;
    const counter = counterState ? counterState + 1 : 1;

    return reply({
      statusCode: 200,
      message: 'get cookie',
      currentState: request.state
    })
    .state('data', {counter: counter}); //<-- You missed to actually SET your cookie!
  }
});

server.ext( 'onPreResponse', ( request, reply ) => {

  //Here you have full access to the original request
  console.log("PreResponse counter value: " + request.state.data.counter);

  return reply.continue();

});

// Start the server
server.start((err) => {

  if (err) {
    throw err;
  }

  console.log('Server running at:', server.info.uri);
});
“严格使用”;
常数Hapi=要求('Hapi');
const Path=require('Path');
//创建具有主机和端口的服务器
const server=new Hapi.server();
服务器连接({
港口:3000
});
//定义cookie
server.state('数据'{
编码:“base64json”
});
//添加路线
服务器路由({
方法:“GET”,
路径:'/getcookie',
处理程序:函数(请求、回复){
const counterState=request.state.data.counter;
常数计数器=反状态?反状态+1:1;
回覆({
状态代码:200,
消息:“获取cookie”,
currentState:request.state
})
.state('data',{counter:counter});//{
//在这里,您可以完全访问原始请求
log(“预响应计数器值:”+request.state.data.counter);
返回reply.continue();
});
//启动服务器
server.start((err)=>{
如果(错误){
犯错误;
}
log('运行在:'的服务器,Server.info.uri);
});
我希望这能帮助您理解如何在hapi中使用cookies,并以某种方式为您的特定问题提供解决方案