node.js socket.io获取cookie值

我想用socket.io在节点服务器中获取cookie

所以每次套接字发送消息到服务器我检查cookie的价值

(我不想要会话cookie)只有在明确我得到的cookie

router.get('/', function(req, res, next) { var cookie_id = req.cookies.userIDf; console.log(cookie_id); res.render('index', { title: 'Express' }); }); 

它的工作在这里,但与socket.io我不能得到cookie任何解决scheme?

 io.sockets.on('connection', function (socket) { var cookief =socket.handshake.headers['cookie']; console.log(cookief); // its give me session id not the cookies }); 

       

网上收集的解决方案 "node.js socket.io获取cookie值"

 io.sockets.on('connection', function (socket) { var cookief =socket.handshake.headers.cookie; var cookies = cookie.parse(socket.handshake.headers.cookie); console.log(cookies); }); 

cookief会给出结果

 io=5J1m_uXsDZvc61gTAAAA; userID=63kfh16tiu7md3ck7djv8856s0; userIDf=50 

所以它需要通过parsing来获得价值

cookie.parse();

cookies.io或cookies.userID或cookies.userIDf

结果将是

 { io: 'RqIksBUQW4SM4zA4AAAA', userID: '63kfh16tiu7md3ck7djv8856s0', userIDf: '50' } 

如果你想要userIDf

cookies.userIDf会给你50