S
S
symblight2017-03-17 01:25:15
Node.js
symblight, 2017-03-17 01:25:15

How to save in Cookies value?

I create a session like this:

import session from 'express-session';
import cookieParser from 'cookie-parser';
import MongoStore from 'connect-mongodb';

app.use(cookieParser());
app.use(session({
    secret:'yes is not bad',
    name : 'sessionId',
    resave: false,
    saveUninitialized: true,
    store: new MongoStore({
        url: connectionOptions
    })
}))

Cookies like this:
router.get('/',(req,res)=>{
    if(!req.cookies.cookieBad){
        let items=[];
        res.cookie('cookieBad', items, {
            expires: new Date(Date.now() + 999999),
            httpOnly: true
        });
 }
});

I add data like this:
router.post('/set',(req,res)=>{
            let items=req.cookies.cookieBad;
            items.push(req.body);
             res.cookie('cookieBad', items, {
                expires: new Date(Date.now() + 999999),
                httpOnly: true
            });
});

After reloading the browser, the values ​​in the cookies are reset to zero ...
How to store the values ​​​​in the cookies permanently so that they are updated only after adding a new value and am I doing the right thing with adding and creating them?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question