Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用hapi设置cookie?_Javascript_Hapijs - Fatal编程技术网

Javascript 如何使用hapi设置cookie?

Javascript 如何使用hapi设置cookie?,javascript,hapijs,Javascript,Hapijs,我阅读了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, e

我阅读了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
});

请在此处提供您的代码,而不是在