Hi guys (Happy new year btw :) )! I'm giving a try to Node.js and Express.js, and, i'm facing some difficulties with the processing of several requests.
Here's the problem : I need to retrieve data from an external website. To do so, I have to be logged in and I have a fake/zombie account available. What I am trying to do is to first send a POST request with my username and password, and then, sending a GET request to access a specific content. The problem is that, even if the POST request is successfully executed, when I'm doing the GET request, I am no longer logged in. I was doing the same thing with the Gem Mechanize in Ruby but, it should probably do some implicit stuff that made the whole process work.
To make requests, I am using the request module (https://github.com/request/request). And, here is the code :
var zombie = require('request').defaults({
headers: { connection: "keep-alive" },
pool: {maxSockets: 1},
});
var connect = function(req, res, next){
zombie.post({
url: "target.com",
form: {
user: "zombie",
pass: "password"}
}, function(err, response, body){
req.connected = true;
next();
});
}
var information = function(req, res, next){
if(req.connected){
zombie.get({
url: "target.com",
qs: {
page: "targetPage",
search: req.params.search}
}, function(err, response, body){
res.send(body);
})
}
}
And, from a controller, I'm trying to connect, then to access information.
var router = require('express').Router();
var zombie = require('./../middleware/Zombie');
router.get('/search/:search', zombie.connect, zombie.information);
Most likely the server is sending back a Set-Cookie
and it's getting ignored (the default behavior for the request
module). To fix that, you need to npm install tough-cookie
and then set up the jar
config option. There are some examples that show various cookie jar usage scenarios.