在无法使用的NodeJS请求和前preSS模块设置在浏览器cookie [英] Unable to set cookie in browser using request and express modules in NodeJS

查看:409
本文介绍了在无法使用的NodeJS请求和前preSS模块设置在浏览器cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端,并写在EX preSS服务器节点。客户端使用请求模块服务器进行通信,服务器将发送响应返回给客户端。在这里,在服务器code,我的cookie发送回客户端。但我无法看到在浏览器中的cookies。在客户端req.cookies是给{}

服务器code:

  VAR前preSS =要求('前preSS');
VAR cookieParser =要求('cookie的解析器');
VAR应用=前preSS();
app.use(cookieParser());
app.get('/测试饼干',函数(REQ,RES){
    。res.cookie('ABC','某某')发送(Cookie设置');
});
app.listen(9000);

客户端code:

  VAR前preSS =要求('前preSS');
VAR请求=要求(请求);
VAR cookieParser =要求('cookie的解析器');
VAR应用=前preSS();
app.use(cookieParser());
app.get('/',函数(REQ,RES){
    请求({
        网址:的http://本地主机:9000 /测试饼干',
        方法:GET
    },功能(错误,响应体){
        如果(错误){
            res.send({
                消息:错误
            });
        }其他{
            如果(200 == response.status code){
                的console.log(req.cookies);
            }
        }
    });
});
app.listen(7000);


解决方案

有关先打客户端浏览器 7000 API将不包含任何的cookie,从而 req.cookies 将登录 {} ,因此,我们必须设置在水库的cookie 客户端浏览器发送Cookie的其他请求。

解决方案(客户code):

 如果(200 == response.status code){    //对于第一个要求它将是空{}
    的console.log(req.cookies);    / *获取与响应报头的饼干
     *和在res设定在浏览器中设置
     * /
    变种C = response.headers ['的Set-Cookie'];
    res.set('的Set-Cookie,C [0]);    res.send('完成');}

说明
你正在做的是试图获取从 REQ 饼干不含有任何的cookie。在响应对象,你是从一下 9000 API将包含该cookie获取。因此,你必须设置从cookie 响应 RES 客户端浏览器。

I am having a client and a server node written in express. The client communicates with server using request module, the server will send response back to the client. Here in server code I am sending cookie back to the client. But I am unable to see the cookies in the browser. In client req.cookies is giving {}

Server code:

var express = require('express');
var cookieParser = require('cookie-parser');
var app = express();
app.use(cookieParser());
app.get('/test-cookie', function(req, res) {
    res.cookie('abc', 'xyz').send('Cookie is set');
});
app.listen(9000);

Client code:

var express = require('express');
var request = require('request');
var cookieParser = require('cookie-parser');
var app = express();
app.use(cookieParser());
app.get('/', function(req, res) {
    request({
        url: 'http://localhost:9000/test-cookie',
        method: 'GET'
    }, function(error, response, body) {
        if (error) {
            res.send({
                msg: error
            });
        } else {
            if (200 == response.statusCode) {
                console.log(req.cookies);
            }
        }
    });
});
app.listen(7000);

解决方案

Client browser for first hit to 7000 api will not contain any cookie, thus req.cookies will log {}, thus we have to set cookie in the res for the client browser to send the cookie for the rest requests.

Solution (Client Code):

if (200 == response.statusCode) {

    // For first request it will be empty {}
    console.log(req.cookies);

    /* fetching the cookie from the response header
     * and setting in res to be set in the browser
     */
    var c = response.headers['set-cookie'];
    res.set('set-cookie',c[0]);

    res.send('Done');

}

Explanation: What you are doing is trying to fetch cookies from req which does not contain any cookie. The response object you are getting from hitting the 9000 api will contain the cookie. Thus, you have to set the cookie from response to the res for client browser.

这篇关于在无法使用的NodeJS请求和前preSS模块设置在浏览器cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆