4

I want to be able to set a cookie and then redirect the client with node.js, without using Express or another third party library. Online I can only find examples using Express.

When I try to set the cookie and redirect the client, the cookie does not get set. But when I comment out the redirect, res.writeHead(301, {Location: serverAddress});, the cookie gets set, but there is no redirect.

So how do I set a cookie and then redirect the client using straight node.js?

var fs = require("fs");
var http = require('http');

var home = fs.readFileSync('random.html');

var serverAddress = "http://yourIpAddress";

http.createServer(function(req, res) {
  if(req.url === '/favicon.ico') {
    res.writeHead(200, {'Content-Type': 'image/x-icon'});
    res.end();

  } else if(req.headers.cookie) {

    console.log("Got cookie");

    res.writeHead(200, "OK", {'Content-Type': 'text/html'});
    res.write(home);
    res.end();

  } else {

    console.log('Creating Cookie');

    var cookie = {
      "some": "data"
    };

    res.writeHead(200, {'Set-Cookie': cookie, 'Content-Type': 'text/plain'});

    res.writeHead(301, {Location: serverAddress});

    res.end();

  }
}).listen(80);
2trill2spill
  • 1,333
  • 2
  • 20
  • 41
  • 1
    is there a reason why you can't use express? – Daemedeor Jun 02 '15 at 19:28
  • I'm confused, wouldn't you have to do `"set-Cookie" : "some=data; Expires=Wed, 09 Jun 2021 10:18:14 GMT"` etc instead of passing in an object, and are you sure `req.headers.cookie` gets populated without a cookieparser middleware. My opinion is that you're reinventing the wheel and should use available middleware. – adeneo Jun 02 '15 at 19:29
  • http://stackoverflow.com/questions/3393854/get-and-set-a-single-cookie-with-node-js-http-server – Daemedeor Jun 02 '15 at 19:29
  • @Daemedeor I saw that link and that only solves getting and setting cookies, not redirecting after. – 2trill2spill Jun 02 '15 at 19:32
  • @adeno, I'm not sure about `"set-Cookie" : "some=data; Expires=Wed, 09 Jun 2021 10:18:14 GMT"`, but `req.headers.cookie` gets set If you comment out the redirect, and run the server twice. – 2trill2spill Jun 02 '15 at 19:35
  • Are you redirecting to a different domain? Cookies expire when the session ends by default, so if you redirect to another domain you're going to lose the cookie unless you set the expiration to a later date. – maembe Jun 02 '15 at 19:49
  • @maembe No, Ia m redirecting back to the same server. – 2trill2spill Jun 02 '15 at 19:50
  • Does this answer your question? [Get and Set a Single Cookie with Node.js HTTP Server](https://stackoverflow.com/questions/3393854/get-and-set-a-single-cookie-with-node-js-http-server) – Antoni Jul 06 '20 at 14:25

2 Answers2

0

To avoid double status setting (200 and 301, I'm not sure if it is possible) You can render html page with META tag HTTP-EQUIV="Refresh" instead of redirectig status, e.g.

var jsrender = require('node-jsrender');

(..)

res.writeHead(200, {'Set-Cookie': cookie, 'Content-Type': 'text/plain'});

// 301 redirection replaced by rendering html template with redirection inside

var tmpl = jsrender.loadFileSync('#redirectTemplate', 'templates/redirect.html')

res.end( tmpl.render() );

And redirect.html looks like this

<!DOCTYPE html>
<html>
<head>
<META HTTP-EQUIV="Refresh" CONTENT="1;URL=/redirect-url">
</head>
</html>    
-1

To redirect a client after setting a cookie without Express, just pass the URL you want to redirect to, to res.address.

var url = "https://someurl.com";    

res.address(url);

res.end();
2trill2spill
  • 1,333
  • 2
  • 20
  • 41