我想做一个非常简单的任务:
- 创建一个 http
request
(npm install request
)http://google.com
并将其 cookie 保存到cookiejar
- 在控制台中查看 cookie 列表
cookiejar
以验证 cookie 确实存在 - 转储所有 cookie
cookiejar
并得到某种确认它已完成(如console.log('no errors')
)
浏览了 strong -cookie的文档后,我在第 2 步和第 3 步时遇到了问题,因为 2 只显示了一个空数组,而 3 则抛出了TypeError: undefined is not a function
我的代码如下:
//npm install request
var request = require('request');
//special request that puts cookies in cookiejar
var cookieRequest = request.defaults( { jar : cookiejar } )
//npm install tough-cookie
var tough = require('tough-cookie');
//request the Store API
var store = tough.Store
var Cookie = tough.Cookie;
var cookiejar = new tough.CookieJar();
//load up google.com, I assume google would set some cookies in cookiejar
cookieRequest("http://google.com",function(error, response, body){
if (!error && response.statusCode==200){
console.log("requestDone")
}
});
//request to see the cookies in cookiejar - at the moment it's returning an empty array,
//would have expected something like ['cookie' : data ]
cookiejar.getCookies("http://google.com",function(err,cookies){
console.log(cookies)
});
//here I want to dump whatever cookies were stored in cookiejar - at the moment this is
//throwing a 'TypeError: undefined is not a function' and can't figure out why
cookiejar.store.removeCookies("http://google.com",function(err){
console.log(err)
});