suchen

Heim  >  Fragen und Antworten  >  Hauptteil

node.js – Über Cookies in node.js

Das ist passiert: Ich habe einen Abschnitt des Codes nicht verstanden, den der Chef heute verloren hat, und ich hatte nicht die Scham, jemanden zu fragen, also musste ich schamlos sein und alle fragen:

router.post('/my_single_article', function(req, res, next) {
    var request = require('./utils/cookie-request').init(req,global.host);
     request.post(global.config.api('my_single_article'),
         {
             form: req.body,
             json:false
         },
         function(error, response, body){
             var result = JSON.parse(body);
             console.log(result+'12324');
                         console.log(global.host);
             res.render('tpl/my_single_article_list',
             {
                   page:result.data,
             });
         }
     );
 });

Ich verstehe diesen Satz nicht ganz

var request = require('./utils/cookie-request').init(req,global.host);

Der zitierte Cookie-Anfrageblock lautet wie folgt:

var request = require('request');
var format = require("string-template");

exports.init = function(req, domain){
    // 保持与请求接口的session一致
    var url = domain || "http://album.laifuzi.cn";
    
    var j = request.jar();
    if(req && req.cookies) {
        for(x in req.cookies){
            var cookie = request.cookie(format('{0}={1}', x, req.cookies[x]));
            j.setCookie(cookie, url);
        }
    }
    return request.defaults({jar:j});
};

Bitte gebt mir ein paar Ratschläge von all den großen Göttern

PHP中文网PHP中文网2792 Tage vor491

Antworte allen(3)Ich werde antworten

  • 天蓬老师

    天蓬老师2017-05-16 13:33:20

    var cookie = request.cookie(format('{0}={1}', x, req.cookies[x]));

    这段没看懂吧?
    format('{0}={1}={2}',1,2,3) => 1=2=3,格式化处理而已

    Antwort
    0
  • 某草草

    某草草2017-05-16 13:33:20

    var request = require('./utils/cookie-request').init(req,global.host);
    // 等价于:
    var request = require('./utils/cookie-request');
    request.init(req,global.host);
    // req和global.host是cookie-request中init的两个参数,req=>req ,global.host=>domain
    exports.init = function(req, domain){
        // 保持与请求接口的session一致
        var url = domain || "http://album.laifuzi.cn";
        
        var j = request.jar();
        if(req && req.cookies) {
            for(x in req.cookies){
                var cookie = request.cookie(format('{0}={1}', x, req.cookies[x]));
                j.setCookie(cookie, url);
            }
        }
        return request.defaults({jar:j});
    };
    

    Antwort
    0
  • 高洛峰

    高洛峰2017-05-16 13:33:20

    var request = require('./utils/cookie-request').init(req,global.host);
    // 等价于:
    var request = require('./utils/cookie-request');
    request = request.init(req,global.host);
    // exports作为commonJS的一种用法,从而可以在别的文件中require这个方法
    exports.init = function(req, domain){
        // 保持与请求接口的session一致
        var url = domain || "http://album.laifuzi.cn";
        
        var j = request.jar();
        //存在req,并且req里面存在cookies
        if(req && req.cookies) {
        遍历req.cookies的属性
            for(x in req.cookies){
            // 这个就是format,楼上说得都没错,cookie的形式一般就是key=value
                var cookie = request.cookie(format('{0}={1}', x, req.cookies[x]));
                j.setCookie(cookie, url);
            }
        }
        return request.defaults({jar:j});
    };

    Antwort
    0
  • StornierenAntwort