redirect url after setting cookie

5,896 views
Skip to first unread message

Neil Camara

unread,
Aug 6, 2014, 9:55:56 AM8/6/14
to nod...@googlegroups.com
Hi folks,

I'm trying to mimic a code that I wrote in php. What the php does is parse GET data request, set the GET data in a cookie then finally redirect the client to another path of the same domain.

The problem  is the "redirect" is not working and I'm getting this message:

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (http.js:689:11)
    at ServerResponse.res.set.res.header (/Users/neil/repos/acme/express/acmechartservice/node_modules/express/lib/response.js:524:10)
    at ServerResponse.res.contentType.res.type (/Users/neil/repos/acme/express/acmechartservice/node_modules/express/lib/response.js:391:15)
    at ServerResponse.res.send (/Users/neil/repos/acme/express/acmechartservice/node_modules/express/lib/response.js:109:43)
    at fn (/Users/neil/repos/acme/express/acmechartservice/node_modules/express/lib/response.js:773:10)
    at View.exports.renderFile [as engine] (/Users/neil/repos/acme/express/acmechartservice/node_modules/jade/lib/jade.js:294:12)
    at View.render (/Users/neil/repos/acme/express/acmechartservice/node_modules/express/lib/view.js:76:8)
    at Function.app.render (/Users/neil/repos/acme/express/acmechartservice/node_modules/express/lib/application.js:502:10)
    at ServerResponse.res.render (/Users/neil/repos/acme/express/acmechartservice/node_modules/express/lib/response.js:777:7)
    at Layer.module.exports [as handle] (/Users/neil/repos/acme/express/acmechartservice/app.js:78:9)

Here is the code

app.get('/sdm', function(req, res){
 
var userData = {
isAuthenticated : true,
username : req.param('email'),
resourceId : req.param('userresourceid'),
bearerToken : req.param('token'),
tokenResourceId : req.param('tokenresourceid'),
firstname : req.param('fname'),
lastname : req.param('lname'),
expirationDate : '2014-12-31T00:00:00.000Z',
locale : 'en'
};
 
res.cookie('credentials', userData).send();
 
});

Any help would be greatly appreciated.

Thanks,

Neil

Aria Stewart

unread,
Aug 6, 2014, 11:35:40 AM8/6/14
to nod...@googlegroups.com
On Aug 6, 2014, at 9:55 AM, Neil Camara <onie....@gmail.com> wrote:
The problem  is the "redirect" is not working and I'm getting this message:

Error: Can't set headers after they are sent.

[snip]

Here is the code

[snip]

res.cookie('credentials', userData).send();
Both 'send' and 'redirect' send the response; you only get to do that once. If you remove the send, the redirect will do it and it will work as you expect.

The cookies will still be sent -- they're queued up as headers, ready to send before the body starts being sent (or in this case, skipped, since redirects have no body)

Aria



Reply all
Reply to author
Forward
0 new messages