我看了hapi.js的官方文档,代码和文档类似,但是无法设置cookie
const Hapi=require('hapi');
const server = Hapi.server({
host:'localhost',
port:3333
});
server.state('user', {
ttl: 24 * 60 * 60 * 1000, // One day
isSecure: true,
isHttpOnly: true,
encoding: 'base64json',
clearInvalid: false,
strictHeader: true
});
server.route({
method:'POST',
path:'/login',
handler:function(request,h) {
var data = request.payload;
if(data.account === "a" && data.password === "a"){
h.state('user', { account: "shmily" });
console.log(request.state.user); //undefined
return h.response('success');
}else{
return "wrong";
}
}
});
async function start() {
try {
await server.start();
}
catch (err) {
process.exit(1);
}
console.log('服务器开启了3333端口!');
};
start();
我想设置cookie但不起作用。我的英语不是很好,希望大家能够宽容一点。
最佳答案
您应该在开发中将 isSecure 更改为 false。当它设置为 true 时,没有安全连接 (https) 时不会设置 cookie。
因此更改您的代码,使其看起来像这样
server.state('user', {
ttl: 24 * 60 * 60 * 1000, // One day
isSecure: false,
isHttpOnly: true,
encoding: 'base64json',
clearInvalid: false,
strictHeader: true
});
关于javascript - 如何使用hapi设置cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49652131/