SORU
3 AĞUSTOS 2010, Salı


Ve node.js Tek bir Çerez Ayarlamak HTTP Server

Tek bir çerez ayarlamak mümkün olmak istiyorum, ve her istek nodejs için yapılan tek kurabiye sunucusu örneğini okudum. Birkaç satır kod olarak hazırlanmış üçüncü parti bir lib çekmek için gerek kalmadan olabilir mi?

var http = require('http');

http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello World\n');
}).listen(8124);

console.log('Server running at http://127.0.0.1:8124/');

Sadece yukarıdaki kodu doğrudan almaya çalışıyor nodejs.org ve içine bir kurabiye çalışması.

CEVAP
4 AĞUSTOS 2010, ÇARŞAMBA


/Şu ile geldim çerez ayarı, hack almak için hızlı işlev erişimi vardır

var http = require('http');

function parseCookies (request) {
    var list = {},
        rc = request.headers.cookie;

    rc && rc.split(';').forEach(function( cookie ) {
        var parts = cookie.split('=');
        list[parts.shift().trim()] = decodeURI(parts.join('='));
    });

    return list;
}


http.createServer(function (request, response) {

  // To Read a Cookie
  var cookies = parseCookies(request);

  // To Write a Cookie
  response.writeHead(200, {
    'Set-Cookie': 'mycookie=test',
    'Content-Type': 'text/plain'
  });
  response.end('Hello World\n');
}).listen(8124);

console.log('Server running at http://127.0.0.1:8124/');

Bu çerezler nesneye tüm çerezleri saklar ve kafasını yazarken çerez ayarlamanız gerekir.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • jeffisthecoolguy

    jeffisthecoo

    17 HAZİRAN 2013
  • Perihelion

    Perihelion

    23 NİSAN 2008
  • Semantic Mastery

    Semantic Mas

    30 EKİM 2013