Linerate Linerate
connect Linerate#connect
Create a session with LineRate
Signature
Linerate.connect = function(opts, opts, opts, opts, opts, callback) { /*...*/ }
Linerate.connect = function(opts, callback) { var self = this; this.base_url = 'https://' + opts.host + ':' + opts.port; this.jar = request.jar(); var options = { url: this.base_url + '/login', headers: { 'content-type': 'application/x-www-form-urlencoded' }, body: 'username=' + opts.username + '&password=' + opts.password, jar: self.jar }; this.request.post(options, function(error, response) { if (error) { //throw new Error('connection error: ' + error); callback(error); } // successful login always returns a 302 if (response.statusCode !== 302) { //throw new Error('Login failure: ' + response.statusCode); callback(error); } // and, conveniently, a failed login attempt returns a 302. // check for 302 and redirect back to login page for failed login if (response.statusCode === 302 && url.parse(response.headers.location).pathname === '/login') { callback('Login failed'); } //console.log(self.jar.getCookies(options.url)); callback(null, 'connected'); }); }
Name | Type(s) | Description |
---|---|---|
opts | Object | The connection options object |
opts | string | .host - Hostname or IP address |
opts | integer | .port |
opts | string? | .username |
opts | string? | .password |
callback | function |
Examples
connect({ host: '127.0.0.1', port: 8443, username: 'admin', password: 'changeme' }, function() { // code here });
put Linerate#put
}