【发布时间】:2017-09-22 17:00:38
【问题描述】:
使用 Hapijs Node 框架,我想确保每个请求都存在某个 cookie。如果它不存在,我想创建它。我想在不手动将该 cookie 添加到每个 reply 的情况下执行此操作。例如,像这样:
server.ext('onPreHandler', function (request, reply) {
console.log(`state: ${JSON.stringify(request.state)}`) // state == null
request.state['my_cookie'] = request.state.my_cookie || 'my data'
return reply.continue();
});
server.route({
method: 'GET',
path: '/',
handler: function(request, reply) {
// includes `my_cookie`
reply(`Cookies: ${JSON.stringify(request.state)}`);
}
})
// cookie does not exist in the browser
将 cookie 添加到回复中是可行的,但需要添加到应用程序中的每个回复中。
server.route({
method: 'GET',
path: '/',
handler: function(request, reply) {
reply(`Cookies: ${JSON.stringify(request.state)}`).state('my_cookie', 'my_data');
}
})
// cookie now exists in the browser
如何确保在所有请求中都添加了 cookie,而无需为每个回复手动添加 cookie?
【问题讨论】:
标签: javascript node.js cookies hapijs