David Geh
David Geh

Reputation: 307

No response after database-access in jugglingdb

I try to use my compound.js-application as a (transparent) proxy-server. When a user tries to request a external website, the application will check, if the user with that ip-address was authenticated before.

If so, the external site will be shown, if not, the user will be encouraged to login. The problem is, that the response is not processed, when there is an access to the database-object "User". When I comment out the database section and just use the code inside the anonymous function, the programm works as expected.

action('exturl', function () {
    User.all({ where: { ipaddress: req.ip }}, function(err, users) {
      if (users.length > 0) {                                                                                              
          this.user = user[0];                                                                                            

          var proxy = http.createClient(80, req.headers['host'])
          var proxy_request = proxy.request(req.method, req.url, req.headers);
          proxy_request.addListener('response', function (proxy_response) {
              proxy_response.addListener('data', function(chunk) {
                  res.write(chunk, 'binary');
              });
              proxy_response.addListener('end', function() {
                  res.end();
              });
              res.writeHead(proxy_response.statusCode, proxy_response.headers);
          });
          req.addListener('data', function(chunk) {
              proxy_request.write(chunk, 'binary');
          });
          req.addListener('end', function() {
              proxy_request.end();
          });
      } else {                                                                                                            
          redirect(path_to.login);                                                                                        
      }                                                                                                                   
    });
});

Is there a failure inside my code? I don't know what I am doing wrong.

Upvotes: 4

Views: 191

Answers (0)

Related Questions