Ad

A Cookie Is Set At Location `/set-cookie` But Cannot Be Retrieved At `/user` Using Express.js

I have a node.js app hosted on firebase. However where it is hosted should not matter too much here as I am simply trying to store some information in cookies in the /set-cookie path so it can be accessed in the /user path. Relevant code in index.ts here:

// create HTTP server
const app = express();

// use json form parser middleware
app.use(bodyParser.json());

// use query string parser middlware
app.use(bodyParser.urlencoded({ extended: true }));


// cookies
app.use(cookieParser());

app.use(cookieSession({
      name   : 'session'
    , keys   : ['abigsecret', 'anothersecret']
    , maxAge : 100 * 60 * 60 * 1000
}));


// setting the cookie here
app.get('/set-cookie', (req,res) => {
    res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });
    res.send('cookie set...');
}})



// And I would like to access it here
app.get('/user' , (req, res) => {
    console.log('cookies: ', req.cookies)
    res.render('pages/user')
});

Of course this is not working as intended. since on the console I do not see the cookie I set in /set-cookie, but rather some client side cookie that was passed to the server. What is the way to set cookies so that I can access it elsewhere?

What I am seeing on the console is this when I navigate to '/user` after '/get-cookie'

info: cookies:  { _ga: 'GA1.1.1192529393.1524186034',
  ss_cvr: '5407c247-08f6-4b6d-8b27-e623c7788eef|1523239108805|1524436640490|1524443386060|46' }
info: 

but I should be seeing some version of : 'rememberme', '1', correct?

Ad

Answer

The following is a full example that works for me.

server.js

// create HTTP server
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const cookieSession = require('cookie-session');
const pug = require('pug');
const app = express();

app.set('views', './views');
app.set('view engine', 'pug');

// use json form parser middleware
app.use(bodyParser.json());

// use query string parser middlware
app.use(bodyParser.urlencoded({ extended: true }));


// cookies
app.use(cookieParser());

app.use(cookieSession({
      name   : 'session'
    , keys   : ['abigsecret', 'anothersecret']
    , maxAge : 100 * 60 * 60 * 1000
}));


// setting the cookie here
app.get('/set-cookie', (req,res) => {
    res.setHeader('Cache-Control', 'private');
    res.cookie('__session', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });
    res.send('cookie set...');
});



// And I would like to access it here
app.get('/user' , (req, res) => {
    console.log(req.cookies);
    res.render('pages/user', {cookies: req.cookies})
});

app.listen(3000, () => { console.log('Listening on port 3000')});

Then views/pages/user.pug:

doctype html
html
  body
    h1 Rememberme => #{cookies.__session}

Navigating in a browser first to http://localhost:3000/set-cookie and then to http://localhost:3000/user works for me. The console.log(req.cookies) includes the remember me and passing it into the template also works.

Ad
source: stackoverflow.com
Ad