无法使用Nodejs的HTTP / HTTPS获取cookie

时间:2017-09-01 02:42:12

标签: node.js cookies node-https

我尝试使用https.request但是当我想要访问cookie响应时,这不会出现在资源标头上。 HTTP / HTTPS。

示例代码:

let https = require('https');

https.request({
    rejectUnauthorized: false,
    hostname: 'example.com',
    port: '443',
    path: '/example.html',
    method: 'POST',
    headers: {
        "Connection": "keep-alive",
        "Accept-Encoding": "gzip, deflate, br",
        "Upgrade-Insecure-Requests": "1",
        "Content-Type": "application/x-www-form-urlencoded",
        "Cache-Control": "no-cache",
        "Pragma": "no-cache",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "Accept-Language": "es-ES,es;q=0.8",
    }
}, (res: any) => {
    console.log('headers', res.headers); // this does not show 'set-cookie' property.
})

支持Cookies吗? 感谢

P.S:我正在使用typescript来编写ts-node来运行此脚本。

1 个答案:

答案 0 :(得分:0)

您可能需要来自npm的cookie-parser包,并添加

const cookieParser = require('cookie-parser');

到app.js文件的顶部,然后

app.use(cookieParser());

到您的中间件。 Cookie将以req.cookies

的形式提供