How to get cookie value in expressjs

后端 未结 3 1723
轮回少年
轮回少年 2020-12-09 08:46

I\'m using cookie-parser, all the tutorial talk about how to set cookie and the time it expiries but no where teach us how to get the value of these cookie

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 09:37

    hope this will help you

    const app = require('express')();
    app.use('/', (req, res) => {
        var cookie = getcookie(req);
        console.log(cookie);
    });
    
    function getcookie(req) {
        var cookie = req.headers.cookie;
        // user=someone; session=QyhYzXhkTZawIb5qSl3KKyPVN (this is my cookie i get)
        return cookie.split('; ');
    }
    

    output

    ['user=someone', 'session=QyhYzXhkTZawIb5qSl3KKyPVN']
    

提交回复
热议问题