简体   繁体   English

Node.js Cookies无法正常工作

[英]Node.js Cookies not working

So I'm hosting a node.js file on my website, and I'm trying to get data through cookies. 因此,我在网站上托管了一个node.js文件,并且试图通过Cookie获取数据。 I am using editmycookie, and I can see that the cookies ARE set. 我正在使用editmycookie,可以看到已设置cookie。

I have a function that gets the cookies from a name 我有一个从名称获取Cookie的功能

function parseCookies(request, finding){
   rc = request.headers.cookie + ';';
   rc && rc.split(';').forEach(function( cookie ) {
      var parts = cookie.split('=');
      if(parts.shift().trim() == finding){
          return decodeURI(parts.join('=')).replace(/-/g, '=');
      }

   });
}

Then I run this, knowing the cookies are set 然后运行此程序,知道已设置cookie

app.get('/', function(req, res){
   user = parseCookies(req, 'user');
   pass = parseCookies(req, 'pass');
   console.log(user + pass);

and it logs as NaN 它记录为NaN

I'm still learning Node, sorry. 我仍在学习Node,抱歉。

I'm on Ubuntu 16.04 if that helps!\\ 如果有帮助,我将使用Ubuntu 16.04!\\

Weirdly enough, it works for some users on my site, and for some it doesn't. 奇怪的是,它对我网站上的某些用户有效,而对某些用户无效。 I haven't noticed a pattern of whose work and whose do not. 我没有注意到谁的工作和谁没有的模式。

You do not return anything from the parseCookies function, the return decodeURI(parts.join('=')).replace(/-/g, '=') is called with the callback of the foreach, so this return is meaningless . 您不会从parseCookies函数中返回任何内容, return decodeURI(parts.join('=')).replace(/-/g, '=')会与foreach的回调一起调用,因此此返回没有意义

Because for that parseCookies returns undefined and undefined + undefined is NaN . 因为为此parseCookies返回undefinedundefined + undefinedNaN

When using forEach you need to save the matched result in an temporary variable result , and return this one. 使用forEach ,需要将匹配的结果保存到一个临时变量result ,并返回该result

function parseCookies(request, finding) {
  var result;
  var rc = request.headers.cookie + ';';
  rc && rc.split(';').forEach(function(cookie) {
    var parts = cookie.split('=');
    if (parts.shift().trim() == finding) {
      result = decodeURI(parts.join('=')).replace(/-/g, '=');
    }
  });

  return result;
}

Or use Array.prototype.find (I didn't have time to test that version so there might be a bug in it): 或使用Array.prototype.find (我没有时间测试该版本,因此其中可能存在错误):

function parseCookies(request, finding) {
  var rc = request.headers.cookie + ';';

  var cookie = rc.split(';').find(cookie => cookie.split('=').shift().trim() == finding);

  return decodeURI(cookie.split('=').join('=')).replace(/-/g, '=');
}

But why do you parse the cookies your self anyway. 但是,为什么您仍然要自己解析Cookie。 There are robust and well tested middlewares that will do this for you. 有功能强大且经过良好测试的中间件将为您完成此任务。

Set the cookies using this code and lets see how it works 使用此代码设置Cookie,然后查看其工作原理

    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/');

And add your routes to which you want to call.Hope this hepls for you. 并添加您要呼叫的路线。希望为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM