访问/ delcookie路由时浏览器挂起?

时间:2013-12-18 22:19:34

标签: node.js cookies express

我可以设置和打印cookie对象,但是当我访问delcookie路径时,我的浏览器会挂起并且不会删除cookie。

我的语法对于delcookie路线是否正确?

var express = require ("express");
var app = express();

app.use( express.cookieParser() );

app.get('/setcookie', function(req, res){
  res.cookie('cookiename', 'cookievalue', { maxAge: 900000, httpOnly: true });
  res.send ('200', "<div style='font-weight:bold; color:blue'>setting cookie</div>");
  console.log (req.cookies);
});

app.get('/printcookie', function(req, res){
  res.send ('200', "<div style='font-weight:bold;'>printing cookie</div>");
  console.log (req.cookies);
});

app.get('/delcookie', function(req, res){
  res.clearCookie('cookiename', { path: '/setcookie' });
});

app.listen(1337);

1 个答案:

答案 0 :(得分:1)

您实际上并未发送回复。 sendCookie方法告诉Express添加一个标题,该标题将在发送响应时删除cookie

添加确实发送回复的内容:

app.get('/delcookie', function(req, res){
  res.clearCookie('cookiename', { path: '/setcookie' });
  res.send('ok');
});