Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何设置cookie?_Node.js_Cookies_Npm_Nightmare - Fatal编程技术网

Node.js 如何设置cookie?

Node.js 如何设置cookie?,node.js,cookies,npm,nightmare,Node.js,Cookies,Npm,Nightmare,我在授权后得到cookies,但我不知道我必须在哪里设置它们以及如何设置它们。我一开始就试着做.cookie.set(),但这不起作用 如何使用已保存的cookie?谢谢。我从节点终端执行了以下操作: var Nightmare = require('nightmare'); var nightmare = Nightmare({ show: true }) nightmare .goto('https://mail.yandex.ru') .type('input[n

我在授权后得到cookies,但我不知道我必须在哪里设置它们以及如何设置它们。我一开始就试着做
.cookie.set()
,但这不起作用


如何使用已保存的cookie?谢谢。

我从节点终端执行了以下操作:

var Nightmare = require('nightmare');
var nightmare = Nightmare({
    show: true
})

nightmare
    .goto('https://mail.yandex.ru')
    .type('input[name=login]', 'mylogin')
    .type('input[name=passwd]', 'mypassword')
    .click('button.nb-button._nb-action-button.nb-group-start')
    .wait('.mail-User-Name')
    .cookies.get()
    .then(function (cookies) {
        //actions
    })

根据
cookies.set()。你说的“这不管用”到底是什么意思?您是否尝试在设置cookie后立即获取cookie,以便查看它是否有效?您能否向我展示一些关于.cookies.set()和.cookies.get()的示例?当然,给我几分钟时间。是的,它有效。但在我的示例中:我使用.set('foo','bar')和.get(),然后授权。授权状态为ok,我关闭进程然后开始新建,我的状态为非授权状态。我将尝试在其他站点执行此操作,问题再次出现,我做错了什么。可能不需要在第二次迭代时使用。set('foo','bar')?噩梦EJS不存储您的cookie,您必须自己存储。我会根据你的需要编辑我的回复。好的,我明白了。谢谢
> var Nightmare = require('nightmare')
undefined
> var nightmare = Nightmare({show:true})
undefined
> nightmare.
... goto('https://google.com').
... cookies.set('foo', 'bar').
... cookies.get().
... then((cookies) => {
...     console.log(JSON.stringify(cookies, null, 4))
... })
Promise { <pending> }
> [
    {
    "name": "NID",
    "value": "96=qo1qY9LTKh1np4OSgiyJTi7e79-_OIoIuc71hnrKWvN1JUnDLJqZlE8u2ij_4mW0-JJhWOCafo5J0j-YkZCFt8H2VHzYUom4cfEd2QLOEsHmAcT2ACx4a5xSvO0SZGZp",
    "domain": ".google.de",
    "hostOnly": false,
    "path": "/",
    "secure": false,
    "httpOnly": true,
    "session": false,
    "expirationDate": 1502733434.077271
    },
    {
    "name": "CONSENT",
    "value": "WP.25d07b",
    "domain": ".google.de",
    "hostOnly": false,
    "path": "/",
    "secure": false,
    "httpOnly": false,
    "session": false,
    "expirationDate": 2145916800.077329
    },
    {
    "name": "foo",
    "value": "bar",
    "domain": "www.google.de",
    "hostOnly": true,
    "path": "/",
    "secure": false,
    "httpOnly": false,
    "session": true
    }
]
var Nightmare = require('nightmare')
var storedCookies // This is where we will store the cookies. It could be stored in a file or database to make it permanent

// First instance:
var nightmare1 = Nightmare({show: true})
nightmare1.
    goto('https://google.com').
    cookies.get().
    then((cookies) => {
        storedCookies = cookies
    })

// Second instance:
var nightmare2 = Nightmare({show: true})

for(var i = 0; i < storedCookies.length; i++)
    nightmare2.
        cookies.set(storedCookies[i].name, storedCookies[i].value)

nightmare2.
    goto('https://google.com')