2

我在 node.js(基于网络信标像素)中编写了一个脚本,需要设置 cookie 并将像素发送回客户端。

我的问题是 cookie 没有设置,如果我从代码中删除发送 GIF 的部分,但我无法找到一种方法让这两个东西一起设置 cookie 并将 GIF 发送回来。

http = require('http');
url = require('url');

http.createServer(function(req, res){
    var requestURL = url.parse(req.url, true);
    if (requestURL.pathname == '/log.gif') {

        // Write a Cookie
        res.writeHead(200, {
            'Set-Cookie' : 'id='+requestURL.query.id+'; expires=' + new Date(new Date().getTime()+86409000).toUTCString()
        });

        var imgHex = '47494638396101000100800000dbdfef00000021f90401000000002c00000000010001000002024401003b';
        var imgBinary = new Buffer(imgHex, 'hex');
        res.writeHead(200, {'Content-Type': 'image/gif' });
        res.end(imgBinary, 'binary');


    } else {
        res.writeHead(200, {'Content-Type': 'text/plain' });
        res.end('');
    }
}).listen(8080);
4

1 回答 1

3
于 2013-03-20T11:31:03.750 回答