Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.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
Node.js 无法为节点设置cookie并使用set-cookiee标头进行反应_Node.js_Reactjs_Express_Session_Cookies - Fatal编程技术网

Node.js 无法为节点设置cookie并使用set-cookiee标头进行反应

Node.js 无法为节点设置cookie并使用set-cookiee标头进行反应,node.js,reactjs,express,session,cookies,Node.js,Reactjs,Express,Session,Cookies,我使用node.js作为后端,并对前端进行响应。我正在使用express session进行会话管理。这是我的注册函数,从单击按钮时的反应调用。它基本上使用fetch调用节点后端api signup = ()=>{ if(this.state.tnc){ fetch(config.BASE_URL+"/api/signup",{ method:'POST', head

我使用node.js作为后端,并对前端进行响应。我正在使用express session进行会话管理。这是我的注册函数,从单击按钮时的反应调用。它基本上使用fetch调用节点后端api

signup = ()=>{
        if(this.state.tnc){
            fetch(config.BASE_URL+"/api/signup",{
                method:'POST',
                headers:{
                    'Content-Type':'application/json',
                },
                body:JSON.stringify(this.state)
            })
            .then(res=>res.json())
            .then(res=>{console.log(document.cookie);console.log(res)})
        }
        else{
            window.alert('Please Accept the Terms and Conditions.')
        }
    }
这就是我在后端初始化会话的方式

router.use(session({
    secret:'secret',
    resave: false,
    saveUninitialized: true,
    cookie:{
        httpOnly:true,
        maxAge : 3600000,
        sameSite : true
    },
    name:'_demo'
}))
这是后端中的注册函数,它是在注册api调用时调用的

signup : (req, res)=>{
        const sess = req.session
        SignUp.findOne({
            where:{phoneNumber:req.body.phoneCode+req.body.phoneNumber}
        }).then(result=>{
            if(result){
                res.send({status:'Success',data:'User Exists'})
            }
            else{
                const user = SignUp.build({
                    fullName:req.body.fullName,
                    email:req.body.email,
                    phoneNumber:req.body.phoneCode+req.body.phoneNumber,
                    aadharNumber:req.body.aadharNumber,
                    city:req.body.city,
                    state:req.body.state,
                    zipCode:req.body.zipCode,
                    country:req.body.country
                });
                user.save().then(result=>{
                    sess.userId = result.getDataValue('id')
                    res.send({status:'Success',data:result.getDataValue('id')})
                })
            }
        })
我正在设置cookiee标题,但未在浏览器中设置。
我得到的答案是,我没有制定正确的cors政策。 在使用中间件将其添加到节点之后

res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
    res.header('Access-Control-Allow-Credentials', true);
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
在前端的fetchapi中设置凭据:“include”为我完成了这项工作

 fetch(config.BASE_URL+"/api/signup",{
                method:'POST',
                credentials: 'include',
                headers:{
                    'Content-Type':'application/json',
                },
                body:JSON.stringify(this.state)
            })

您的会话id已作为
\u demo
cookie发送到浏览器。你还希望在浏览器中得到什么?我在标题中得到了它,但是当我在cookiee部分的开发工具中看到时,我没有看到cookie被设置。另外,当我打印document.cookie时,它不会被打印。请将
httpOnly
的值更改为
false
。否则,浏览器不允许您从JS代码访问cookie