最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cookies undefined on node jsexpress (localhost) - Stack Overflow

programmeradmin4浏览0评论

A Newbie question I guess. I want to set and get cookies on my express site.

cookieParser is set up and seems to run. But my cookies are always undefined. So what can be wrong? Doesn't cookies work on localhost? I can access all cookies in the console on chrome.

I have tried both httpOnly: false/true.

Here's my code:

var express = require('express'),
    exphbs = require('express-handlebars'),
    bodyParser = require('body-parser'),
    cookieParser = require('cookie-parser'),
    request = require('request'),
    livereload = require('express-livereload'),
    port = Number(process.env.PORT || 3000);

var log = require('./lib/log.js'));

var app = express();

livereload(app, config = {watchDir: process.cwd()});

app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');

app.use(express.static('public'));
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: true}));

app.get('/', function(req, res) {

    res
        .cookie('cart', 'test', {maxAge: 900000, httpOnly: false})
        .render('index');

    console.log(res.cookie.cart);

});


app.listen(port, function() {

    log.clear();
    log.out('Express on http://localhost:' + port);
    log.hr();

});

Any clues?

A Newbie question I guess. I want to set and get cookies on my express site.

cookieParser is set up and seems to run. But my cookies are always undefined. So what can be wrong? Doesn't cookies work on localhost? I can access all cookies in the console on chrome.

I have tried both httpOnly: false/true.

Here's my code:

var express = require('express'),
    exphbs = require('express-handlebars'),
    bodyParser = require('body-parser'),
    cookieParser = require('cookie-parser'),
    request = require('request'),
    livereload = require('express-livereload'),
    port = Number(process.env.PORT || 3000);

var log = require('./lib/log.js'));

var app = express();

livereload(app, config = {watchDir: process.cwd()});

app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');

app.use(express.static('public'));
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: true}));

app.get('/', function(req, res) {

    res
        .cookie('cart', 'test', {maxAge: 900000, httpOnly: false})
        .render('index');

    console.log(res.cookie.cart);

});


app.listen(port, function() {

    log.clear();
    log.out('Express on http://localhost:' + port);
    log.hr();

});

Any clues?

Share Improve this question edited Sep 21, 2016 at 12:38 arpo asked Sep 21, 2016 at 11:35 arpoarpo 1,9092 gold badges28 silver badges51 bronze badges 2
  • Can you post your app.js file code? – abdulbari Commented Sep 21, 2016 at 11:38
  • Sure! Just updated the post. – arpo Commented Sep 21, 2016 at 11:41
Add a ment  | 

1 Answer 1

Reset to default 5

Maybe you should change:

console.log(res.cookie.cart);

to:

console.log(req.cookies.cart);

I just wrote a simple example that demonstrates what's going on:

var express = require('express');
var cookieParser = require('cookie-parser');
var app = express();
app.use(cookieParser());
app.get('/', function(req, res) {
  var oldCookie = req.cookies.test;
  var newCookie = (oldCookie|0) + 1;
  res.cookie('test', newCookie, {maxAge: 900000});
  res.status(200).json({
    newCookie: newCookie,
    oldCookie: oldCookie,
    reqCookie: req.cookies.test,
  });
});
app.listen(3000, function () {
  console.log('Listening on http://localhost:3000/');
});

When you run it and go with your browser to http://localhost:3000/ you will see:

{"newCookie":1}

When you reload the page you will see:

{"newCookie":2,"oldCookie":"1","reqCookie":"1"}

Here's what's going on: In the first request even though you set the cookie in you handler before printing it it is not really set yet - it is just queued to be passed to the client in the response with an HTTP header like this one:

Set-Cookie: test=1; Max-Age=900; Path=/; Expires=Wed, 21 Sep 2016 13:03:06 GMT

In the second request you see the old value in reqCookie and the new value in newCookie - those values are different. Seeting the cookie doesn't change the one that you got in the request. I even included the reqCookie which is not stored in a variable but accessed directly from req.cookies during the res.end() invocation to demonstrate that it is not changed.

发布评论

评论列表(0)

  1. 暂无评论