package/package.json000666 000000 000000 0000001045 13046356534013002 0ustar00000000 000000 { "author": "Alejandro Alvarez Acero", "name": "node-rest-client", "description": "node API REST client", "version": "2.5.0", "repository": { "type":"git", "url": "https://github.com/aacerox/node-rest-client.git" }, "main": "./lib/node-rest-client", "dependencies": { "xml2js":">=0.2.4", "debug": "~2.2.0", "follow-redirects":">=1.2.0" }, "devDependencies": { "jasmine-node":">=1.2.3" }, "optionalDependencies": {}, "engines": { "node": "*" }, "license": "MIT" } package/.npmignore000666 000000 000000 0000000170 13023770361012502 0ustar00000000 000000 test/man-test.js test/spec/test.js node_modules *.log test/multiple-clients-test.js test/manual-test.js package/LICENSE000666 000000 000000 0000002122 13023770361011507 0ustar00000000 000000 Copyright (c) 2013 Alejandro Alvarez Acero. All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.package/lib/node-rest-client.js000666 000000 000000 0000051512 13024534773014777 0ustar00000000 000000 var http = require('follow-redirects').http, https = require('follow-redirects').https, parseString = require('xml2js').parseString, urlParser = require('url'), util = require("util"), events = require("events"), zlib = require("zlib"), node_debug = require("debug")("NRC"); exports.Client = function (options){ var self = this; var connectManager = new ConnectManager(); //declare util constants var CONSTANTS={ HEADER_CONTENT_LENGTH:"Content-Length" }; self.options = options || {}, self.useProxy = (self.options.proxy || false)?true:false, self.useProxyTunnel = (!self.useProxy || self.options.proxy.tunnel===undefined)?false:self.options.proxy.tunnel, self.proxy = self.options.proxy, self.connection = self.options.connection || {}, self.mimetypes = self.options.mimetypes || {}, self.requestConfig = self.options.requestConfig || {}, self.responseConfig = self.options.responseConfig || {}; this.methods={}; // Client Request to be passed to ConnectManager and returned // for each REST method invocation var ClientRequest =function(){ events.EventEmitter.call(this); }; util.inherits(ClientRequest, events.EventEmitter); ClientRequest.prototype.end = function(){ if(this._httpRequest) { this._httpRequest.end(); } }; ClientRequest.prototype.setHttpRequest=function(req){ this._httpRequest = req; }; var Util = { createProxyPath:function(url){ var result = url.host; // check url protocol to set path in request options if (url.protocol === "https:"){ // port is set, leave it, otherwise use default https 443 result = (url.host.indexOf(":") == -1?url.hostname + ":443":url.host); } return result; }, createProxyHeaders:function(url){ var result ={}; // if proxy requires authentication, create Proxy-Authorization headers if (self.proxy.user && self.proxy.password){ result["Proxy-Authorization"] = "Basic " + new Buffer([self.proxy.user,self.proxy.password].join(":")).toString("base64"); } // no tunnel proxy connection, we add the host to the headers if(!self.useProxyTunnel) result["host"] = url.host; return result; }, createConnectOptions:function(connectURL, connectMethod){ debug("connect URL = ", connectURL); var url = urlParser.parse(connectURL), path, result={}, protocol = url.protocol.indexOf(":") == -1?url.protocol:url.protocol.substring(0,url.protocol.indexOf(":")), defaultPort = protocol === 'http'?80:443; result ={ host: url.host.indexOf(":") == -1?url.host:url.host.substring(0,url.host.indexOf(":")), port: url.port === undefined?defaultPort:url.port, path: url.path, protocol:protocol, href:url.href }; if (self.useProxy) result.agent = false; // cannot use default agent in proxy mode if (self.options.user && self.options.password){ result.auth = [self.options.user,self.options.password].join(":"); } else if (self.options.user && !self.options.password){ // some sites only needs user with no password to authenticate result.auth = self.options.user + ":"; } // configure proxy connection to establish a tunnel if (self.useProxy){ result.proxy ={ host: self.proxy.host, port: self.proxy.port, method: self.useProxyTunnel?'CONNECT':connectMethod,//if proxy tunnel use 'CONNECT' method, else get method from request, path: self.useProxyTunnel?this.createProxyPath(url):connectURL, // if proxy tunnel set proxy path else get request path, headers: this.createProxyHeaders(url) // createProxyHeaders add correct headers depending of proxy connection type }; } if(self.connection && typeof self.connection === 'object'){ for(var option in self.connection){ result[option] = self.connection[option]; } } // don't use tunnel to connect to proxy, direct request // and delete proxy options if (!self.useProxyTunnel){ for (option in result.proxy){ result[option] = result.proxy[option]; } delete result.proxy; } // add general request and response config to connect options result.requestConfig = self.requestConfig; result.responseConfig = self.responseConfig; return result; }, decodeQueryFromURL: function(connectURL){ var url = urlParser.parse(connectURL), query = url.query.substring(1).split("&"), keyValue, result={}; // create decoded args from key value elements in query+ for (var i=0;i 1 ? "&": "") + key + "=" + encodeURIComponent(args[key][ii])); counter++; } } else { //No array, just a single &key=value keyValue = key + "=" + encodeURIComponent(args[key]); result = result.concat((counter > 1 ? "&":"") + keyValue); } counter++; } return result; }, parsePathParameters:function(args,url){ var result = url; if (!args || !args.path) return url; for (var placeholder in args.path){ var regex = new RegExp("\\$\\{" + placeholder + "\\}","i"); result = result.replace(regex,args.path[placeholder]); } return result; }, overrideClientConfig:function(connectOptions,methodOptions){ function validateReqResOptions(reqResOption){ return (reqResOption && typeof reqResOption === 'object'); } // check if we have particular request or response config set on this method invocation // and override general request/response config if (validateReqResOptions(methodOptions.requestConfig)){ util._extend(connectOptions.requestConfig,methodOptions.requestConfig); } if (validateReqResOptions(methodOptions.responseConfig)){ util._extend(connectOptions.responseConfig,methodOptions.responseConfig); } }, connect : function(method, url, args, callback, clientRequest){ // configure connect options based on url parameter parse var options = this.createConnectOptions(this.parsePathParameters(args,url), method); debug("options pre connect",options); options.method = method, clientRequest.href=options.href, options.clientRequest = clientRequest, options.headers= options.headers || {}; debug("args = ", args); debug("args.data = ", args !== undefined?args.data:undefined); // no args passed if (typeof args === 'function'){ callback = args; //add Content-length to POST/PUT/DELETE/PATCH methods if (method === 'POST' || method === 'PUT' || method === 'DELETE' || method === 'PATCH'){ options.headers[CONSTANTS.HEADER_CONTENT_LENGTH] = 0; } } else if (typeof args === 'object') { // add headers and POST/PUT/DELETE/PATCH data to connect options to be passed // with request, but without deleting other headers like non-tunnel proxy headers if (args.headers){ for (var headerName in args.headers){ if (args.headers.hasOwnProperty(headerName)) { options.headers[headerName] = args.headers[headerName]; } } } //always set Content-length header if not set previously //set Content lentgh for some servers to work (nginx, apache) if (args.data !== undefined && !options.headers.hasOwnProperty(CONSTANTS.HEADER_CONTENT_LENGTH)){ options.data = args.data; options.headers[CONSTANTS.HEADER_CONTENT_LENGTH] = Buffer.byteLength((typeof args.data === 'string' ? args.data:JSON.stringify(args.data)), 'utf8'); }else{ options.headers[CONSTANTS.HEADER_CONTENT_LENGTH] = 0; } // we have args, go and check if we have parameters if (args.parameters && Object.keys(args.parameters).length > 0){ // validate URL consistency, and fix it options.path +=(options.path.charAt(url.length-1) === '?'?"?":""); options.path = options.path.concat(Util.encodeQueryFromArgs(args.parameters)); debug("options.path after request parameters = ", options.path); } // override client config, by the moment just for request response config this.overrideClientConfig(options,args); } debug("options post connect",options); debug("FINAL SELF object ====>", self); if (self.useProxy && self.useProxyTunnel){ connectManager.proxy(options,callback); }else{ // normal connection and direct proxy connections (no tunneling) connectManager.normal(options,callback); } }, mergeMimeTypes:function(mimetypes){ // merge mime-types passed as options to client if (mimetypes && typeof mimetypes === "object"){ if (mimetypes.json && mimetypes.json instanceof Array && mimetypes.json.length > 0){ connectManager.jsonctype = mimetypes.json; }else if (mimetypes.xml && mimetypes.xml instanceof Array && mimetypes.xml.length > 0){ connectManager.xmlctype = mimetypes.xml; } } }, createHttpMethod:function(methodName){ return function(url, args, callback){ var clientRequest = new ClientRequest(); Util.connect(methodName.toUpperCase(), url, args, callback, clientRequest); return clientRequest; }; } }, Method = function(url, method){ var httpMethod = self[method.toLowerCase()]; return function(args,callback){ var completeURL = url; //no args if (typeof args === 'function'){ callback = args; args = {}; }else if (typeof args === 'object'){ // we have args, go and check if we have parameters if (args.parameters && Object.keys(args.parameters).length > 0){ // validate URL consistency, and fix it url +=(url.charAt(url.length-1) === '?'?"?":""); completeURL = url.concat(Util.encodeQueryFromArgs(args.parameters)); //delete args parameters we don't need it anymore in registered // method invocation delete args.parameters; } } return httpMethod(completeURL, args , callback); }; }; this.get = Util.createHttpMethod("get"); this.post = Util.createHttpMethod("post"); this.put = Util.createHttpMethod("put"); this.delete = Util.createHttpMethod("delete"); this.patch = Util.createHttpMethod("patch"); this.registerMethod = function(name, url, method){ // create method in method registry with preconfigured REST invocation // method this.methods[name] = new Method(url,method); }; this.unregisterMethod = function(name){ delete this.methods[name]; }; this.addCustomHttpMethod=function(methodName){ self[methodName.toLowerCase()] = Util.createHttpMethod(methodName); } // handle ConnectManager events connectManager.on('error',function(err){ self.emit('error',err); }); // merge mime types with connect manager Util.mergeMimeTypes(self.mimetypes); debug("ConnectManager", connectManager); }; var ConnectManager = function() { this.xmlctype = ["application/xml","application/xml;charset=utf-8","text/xml","text/xml;charset=utf-8"]; this.jsonctype = ["application/json","application/json;charset=utf-8"], this.isXML = function(content){ var result = false; if (!content) return result; for (var i=0; i 0); }, this.configureRequest = function(req, config, clientRequest){ if (config.timeout){ req.setTimeout(config.timeout, function(){ clientRequest.emit('requestTimeout',req); }); } if(config.noDelay) req.setNoDelay(config.noDelay); if(config.keepAlive) req.setSocketKeepAlive(config.noDelay,config.keepAliveDelay || 0); }, this.configureResponse = function(res,config, clientRequest){ if (config.timeout){ res.setTimeout(config.timeout, function(){ clientRequest.emit('responseTimeout',res); res.close(); }); } }, this.handleEnd = function(res,buffer,callback){ var self = this, content = res.headers["content-type"], encoding = res.headers["content-encoding"]; debug("content-type: ", content); debug("content-encoding: ",encoding); if(encoding !== undefined && encoding.indexOf("gzip") >= 0){ debug("gunzip"); zlib.gunzip(Buffer.concat(buffer),function(er,gunzipped){ self.handleResponse(res,gunzipped,callback); }); }else if(encoding !== undefined && encoding.indexOf("deflate") >= 0){ debug("inflate"); zlib.inflate(Buffer.concat(buffer),function(er,inflated){ self.handleResponse(res,inflated,callback); }); }else { debug("not compressed"); self.handleResponse(res,Buffer.concat(buffer),callback); } }, this.handleResponse = function(res,data,callback){ var content = res.headers["content-type"] && res.headers["content-type"].replace(/ /g, ''); debug("response content is [",content,"]"); // XML data need to be parsed as JS object if (this.isXML(content)){ debug("detected XML response"); parseString(data.toString(), function (err, result) { callback(result, res); }); }else if (this.isJSON(content)){ debug("detected JSON response"); var jsonData, data = data.toString(); try { jsonData = this.isValidData(data)?JSON.parse(data):data; } catch (err) { // Something went wrong when parsing json. This can happen // for many reasons, including a bad implementation on the // server. jsonData = 'Error parsing response. response: [' + data + '], error: [' + err + ']'; } callback(jsonData, res); }else{ debug("detected unknown response"); callback(data, res); } }, this.prepareData = function(data){ var result; if ((data instanceof Buffer) || (typeof data !== 'object')){ result = data; }else{ result = JSON.stringify(data); } return result; }, this.proxy = function(options, callback){ debug("proxy options",options.proxy); // creare a new proxy tunnel, and use to connect to API URL var proxyTunnel = http.request(options.proxy), self = this; proxyTunnel.on('connect',function(res, socket, head){ debug("proxy connected",socket); // set tunnel socket in request options, that's the tunnel itself options.socket = socket; var buffer=[], protocol = (options.protocol =="http")?http:https, clientRequest = options.clientRequest, requestConfig = options.requestConfig, responseConfig = options.responseConfig; //remove "protocol" and "clientRequest" option from options, cos is not allowed by http/hppts node objects delete options.protocol; delete options.clientRequest; delete options.requestConfig; delete options.responseConfig; // add request options to request returned to calling method clientRequest.options = options; var request = protocol.request(options, function(res){ //configure response self.configureResponse(res,responseConfig, clientRequest); // concurrent data chunk handler res.on('data',function(chunk){ buffer.push(new Buffer(chunk)); }); res.on('end',function(){ self.handleEnd(res,buffer,callback); }); // handler response errors res.on('error',function(err){ if (clientRequest !== undefined && typeof clientRequest === 'object'){ // add request as property of error err.request = clientRequest; err.response = res; // request error handler clientRequest.emit('error',err); }else{ // general error handler self.emit('error',err); } }); }); // configure request and add it to clientRequest // and add it to request returned self.configureRequest(request,requestConfig, clientRequest); clientRequest.setHttpRequest(request); // write POST/PUT data to request body; if(options.data) request.write(self.prepareData(options.data)); // handle request errors and handle them by request or general error handler request.on('error',function(err){ if (clientRequest !== undefined && typeof clientRequest === 'object'){ // add request as property of error err.request = clientRequest; // request error handler clientRequest.emit('error',err); }else{ // general error handler self.emit('error',err); } }); request.end(); }); // proxy tunnel error are only handled by general error handler proxyTunnel.on('error',function(e){ self.emit('error',e); }); proxyTunnel.end(); }, this.normal = function(options, callback){ var buffer = [], protocol = (options.protocol === "http")?http:https, clientRequest = options.clientRequest, requestConfig = options.requestConfig, responseConfig = options.responseConfig, self = this; //remove "protocol" and "clientRequest" option from options, cos is not allowed by http/hppts node objects delete options.protocol; delete options.clientRequest; delete options.requestConfig; delete options.responseConfig; debug("options pre connect", options); // add request options to request returned to calling method clientRequest.options = options; var request = protocol.request(options, function(res){ //configure response self.configureResponse(res,responseConfig, clientRequest); // concurrent data chunk handler res.on('data',function(chunk){ buffer.push(new Buffer(chunk)); }); res.on('end',function(){ self.handleEnd(res,buffer,callback); }); // handler response errors res.on('error',function(err){ if (clientRequest !== undefined && typeof clientRequest === 'object'){ // add request as property of error err.request = clientRequest; err.response = res; // request error handler clientRequest.emit('error',err); }else{ // general error handler self.emit('error',err); } }); }); // configure request and add it to clientRequest // and add it to request returned self.configureRequest(request,requestConfig, clientRequest); debug("clientRequest",clientRequest); clientRequest.setHttpRequest(request); // handle request errors and handle them by request or general error handler request.on('error',function(err){ debug('request error', clientRequest); if (clientRequest !== undefined && typeof clientRequest === 'object'){ // add request as property of error err.request = clientRequest; // request error handler clientRequest.emit('error',err); }else{ // general error handler self.emit('error',err); } }); debug("options data", options.data); // write POST/PUT data to request body; if(options.data) request.write(this.prepareData(options.data)); request.end(); } }; // event handlers for client and ConnectManager util.inherits(exports.Client, events.EventEmitter); util.inherits(ConnectManager,events.EventEmitter); var debug = function(){ if (!process.env.DEBUG) return; var now = new Date(), header =now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds() + " [NRC CLIENT]" + arguments.callee.caller.name + " -> ", args = Array.prototype.slice.call(arguments); args.splice(0,0,header); node_debug.apply(console,args); }; package/readme.md000666 000000 000000 0000034502 13024542271012266 0ustar00000000 000000 # REST Client for Node.js [![npm version](https://badge.fury.io/js/node-rest-client.svg)](https://www.npmjs.com/package/node-rest-client) [![Build Status](https://travis-ci.org/olalonde/node-rest-client.svg?branch=master)](https://travis-ci.org/olalonde/node-rest-client) [![NPM](https://nodei.co/npm/node-rest-client.png?downloads=true)](https://nodei.co/npm/node-rest-client.png?downloads=true) **NOTE:** _Since version 0.8.0 node does not contain node-waf anymore. The node-zlib package which node-rest-client make use of, depends on node-waf.Fortunately since version 0.8.0 zlib is a core dependency of node, so since version 1.0 of node-rest-client the explicit dependency to "zlib" has been removed from package.json. therefore if you are using a version below 0.8.0 of node please use a versión below 1.0.0 of "node-rest-client". _ Allows connecting to any API REST and get results as js Object. The client has the following features: - Transparent HTTP/HTTPS connection to remote API sites. - Allows simple HTTP basic authentication. - Allows most common HTTP operations: GET, POST, PUT, DELETE, PATCH. - Allows creation of custom HTTP Methods (PURGE, etc.) - Direct or through proxy connection to remote API sites. - Register remote API operations as client own methods, simplifying reuse. - Automatic parsing of XML and JSON response documents as js objects. - Dynamic path and query parameters and request headers. - Improved Error handling mechanism (client or specific request) - Added support for compressed responses: gzip and deflate - Added support for follow redirects thanks to great [follow-redirects](https://www.npmjs.com/package/follow-redirects) package ## Installation $ npm install node-rest-client ## Usages ### Simple HTTP GET Client has 2 ways to call a REST service: direct or using registered methods ```javascript var Client = require('node-rest-client').Client; var client = new Client(); // direct way client.get("http://remote.site/rest/xml/method", function (data, response) { // parsed response body as js object console.log(data); // raw response console.log(response); }); // registering remote methods client.registerMethod("jsonMethod", "http://remote.site/rest/json/method", "GET"); client.methods.jsonMethod(function (data, response) { // parsed response body as js object console.log(data); // raw response console.log(response); }); ``` ### HTTP POST POST, PUT or PATCH method invocation are configured like GET calls with the difference that you have to set "Content-Type" header in args passed to client method invocation: ```javascript //Example POST method invocation var Client = require('node-rest-client').Client; var client = new Client(); // set content-type header and data as json in args parameter var args = { data: { test: "hello" }, headers: { "Content-Type": "application/json" } }; client.post("http://remote.site/rest/xml/method", args, function (data, response) { // parsed response body as js object console.log(data); // raw response console.log(response); }); // registering remote methods client.registerMethod("postMethod", "http://remote.site/rest/json/method", "POST"); client.methods.postMethod(args, function (data, response) { // parsed response body as js object console.log(data); // raw response console.log(response); }); ``` If no "Content-Type" header is set as client arg POST,PUT and PATCH methods will not work properly. ### Passing args to registered methods You can pass diferents args to registered methods, simplifying reuse: path replace parameters, query parameters, custom headers ```javascript var Client = require('node-rest-client').Client; // direct way var client = new Client(); var args = { data: { test: "hello" }, // data passed to REST method (only useful in POST, PUT or PATCH methods) path: { "id": 120 }, // path substitution var parameters: { arg1: "hello", arg2: "world" }, // query parameter substitution vars headers: { "test-header": "client-api" } // request headers }; client.get("http://remote.site/rest/json/${id}/method?arg1=hello&arg2=world", args, function (data, response) { // parsed response body as js object console.log(data); // raw response console.log(response); }); // registering remote methods client.registerMethod("jsonMethod", "http://remote.site/rest/json/${id}/method", "GET"); /* this would construct the following URL before invocation * * http://remote.site/rest/json/120/method?arg1=hello&arg2=world * */ client.methods.jsonMethod(args, function (data, response) { // parsed response body as js object console.log(data); // raw response console.log(response); }); ``` You can even use path placeholders in query string in direct connection: ```javascript var Client = require('node-rest-client').Client; // direct way var client = new Client(); var args = { path: { "id": 120, "arg1": "hello", "arg2": "world" }, parameters: { arg1: "hello", arg2: "world" }, headers: { "test-header": "client-api" } }; client.get("http://remote.site/rest/json/${id}/method?arg1=${arg1}&arg2=${arg2}", args, function (data, response) { // parsed response body as js object console.log(data); // raw response console.log(response); }); ``` ### HTTP POST and PUT methods To send data to remote site using POST or PUT methods, just add a data attribute to args object: ```javascript var Client = require('node-rest-client').Client; // direct way var client = new Client(); var args = { path: { "id": 120 }, parameters: { arg1: "hello", arg2: "world" }, headers: { "test-header": "client-api" }, data: "helloworld" }; client.post("http://remote.site/rest/xml/${id}/method?arg1=hello&arg2=world", args, function (data, response) { // parsed response body as js object console.log(data); // raw response console.log(response); }); // registering remote methods client.registerMethod("xmlMethod", "http://remote.site/rest/xml/${id}/method", "POST"); client.methods.xmlMethod(args, function (data, response) { // parsed response body as js object console.log(data); // raw response console.log(response); }); // posted data can be js object var args_js = { path: { "id": 120 }, parameters: { arg1: "hello", arg2: "world" }, headers: { "test-header": "client-api" }, data: { "arg1": "hello", "arg2": 123 } }; client.methods.xmlMethod(args_js, function (data, response) { // parsed response body as js object console.log(data); // raw response console.log(response); }); ``` ### Request/Response configuration It's also possible to configure each request and response, passing its configuration as an additional argument in method call. ```javascript var client = new Client(); // request and response additional configuration var args = { path: { "id": 120 }, parameters: { arg1: "hello", arg2: "world" }, headers: { "test-header": "client-api" }, data: "helloworld", requestConfig: { timeout: 1000, //request timeout in milliseconds noDelay: true, //Enable/disable the Nagle algorithm keepAlive: true, //Enable/disable keep-alive functionalityidle socket. keepAliveDelay: 1000 //and optionally set the initial delay before the first keepalive probe is sent }, responseConfig: { timeout: 1000 //response timeout } }; client.post("http://remote.site/rest/xml/${id}/method?arg1=hello&arg2=world", args, function (data, response) { // parsed response body as js object console.log(data); // raw response console.log(response); }); ``` If you want to handle timeout events both in the request and in the response just add a new "requestTimeout" or "responseTimeout" event handler to clientRequest returned by method call. ```javascript var client = new Client(); // request and response additional configuration var args = { path: { "id": 120 }, parameters: { arg1: "hello", arg2: "world" }, headers: { "test-header": "client-api" }, data: "helloworld", requestConfig: { timeout: 1000, //request timeout in milliseconds noDelay: true, //Enable/disable the Nagle algorithm keepAlive: true, //Enable/disable keep-alive functionalityidle socket. keepAliveDelay: 1000 //and optionally set the initial delay before the first keepalive probe is sent }, responseConfig: { timeout: 1000 //response timeout } }; var req = client.post("http://remote.site/rest/xml/${id}/method?arg1=hello&arg2=world", args, function (data, response) { // parsed response body as js object console.log(data); // raw response console.log(response); }); req.on('requestTimeout', function (req) { console.log('request has expired'); req.abort(); }); req.on('responseTimeout', function (res) { console.log('response has expired'); }); //it's usefull to handle request errors to avoid, for example, socket hang up errors on request timeouts req.on('error', function (err) { console.log('request error', err); }); ``` ### Follows Redirect Node REST client follows redirects by default to a maximum of 21 redirects, but it's also possible to change follows redirect default config in each request done by the client ```javascript var client = new Client(); // request and response additional configuration var args = { requestConfig: { followRedirects:true,//whether redirects should be followed(default,true) maxRedirects:10//set max redirects allowed (default:21) }, responseConfig: { timeout: 1000 //response timeout } }; ``` ### Connect through proxy Just pass proxy configuration as option to client. ```javascript var Client = require('node-rest-client').Client; // configure proxy var options_proxy = { proxy: { host: "proxy.foo.com", port: 8080, user: "proxyuser", password: "123", tunnel: true } }; var client = new Client(options_proxy); ``` client has 2 ways to connect to target site through a proxy server: tunnel or direct request, the first one is the default option so if you want to use direct request you must set tunnel off. ```javascript var Client = require('node-rest-client').Client; // configure proxy var options_proxy = { proxy: { host: "proxy.foo.com", port: 8080, user: "proxyuser", password: "123", tunnel: false // use direct request to proxy } }; var client = new Client(options_proxy); ``` ### Basic HTTP auth Just pass username and password or just username, if no password is required by remote site, as option to client. Every request done with the client will pass username and password or just username if no password is required as basic authorization header. ```javascript var Client = require('node-rest-client').Client; // configure basic http auth for every request var options_auth = { user: "admin", password: "123" }; var client = new Client(options_auth); ``` ### Options parameters You can pass the following args when creating a new client: ```javascript var options = { // proxy configuration proxy: { host: "proxy.foo.com", // proxy host port: 8080, // proxy port user: "ellen", // proxy username if required password: "ripley" // proxy pass if required }, // aditional connection options passed to node http.request y https.request methods // (ie: options to connect to IIS with SSL) connection: { secureOptions: constants.SSL_OP_NO_TLSv1_2, ciphers: 'ECDHE-RSA-AES256-SHA:AES256-SHA:RC4-SHA:RC4:HIGH:!MD5:!aNULL:!EDH:!AESGCM', honorCipherOrder: true }, // customize mime types for json or xml connections mimetypes: { json: ["application/json", "application/json;charset=utf-8"], xml: ["application/xml", "application/xml;charset=utf-8"] }, user: "admin", // basic http auth username if required password: "123", // basic http auth password if required requestConfig: { timeout: 1000, //request timeout in milliseconds noDelay: true, //Enable/disable the Nagle algorithm keepAlive: true, //Enable/disable keep-alive functionalityidle socket. keepAliveDelay: 1000 //and optionally set the initial delay before the first keepalive probe is sent }, responseConfig: { timeout: 1000 //response timeout } }; ``` Note that requestConfig and responseConfig options if set on client instantiation apply to all of its requests/responses and is only overriden by request or reponse configs passed as args in method calls. ### Managing Requests Each REST method invocation returns a request object with specific request options and error, requestTimeout and responseTimeout event handlers. ```javascript var Client = require('node-rest-client').Client; var client = new Client(); var args = { requesConfig: { timeout: 1000 }, responseConfig: { timeout: 2000 } }; // direct way var req1 = client.get("http://remote.site/rest/xml/method", args, function (data, response) { // parsed response body as js object console.log(data); // raw response console.log(response); }); // view req1 options console.log(req1.options); req1.on('requestTimeout', function (req) { console.log("request has expired"); req.abort(); }); req1.on('responseTimeout', function (res) { console.log("response has expired"); }); // registering remote methods client.registerMethod("jsonMethod", "http://remote.site/rest/json/method", "GET"); var req2 = client.methods.jsonMethod(function (data, response) { // parsed response body as js object console.log(data); // raw response console.log(response); }); // handling specific req2 errors req2.on('error', function (err) { console.log('something went wrong on req2!!', err.request.options); }); ``` ### Error Handling Now you can handle error events in two places: on client or on each request. ```javascript var client = new Client(options_auth); // handling request error events client.get("http://remote.site/rest/xml/method", function (data, response) { // parsed response body as js object console.log(data); // raw response console.log(response); }).on('error', function (err) { console.log('something went wrong on the request', err.request.options); }); // handling client error events client.on('error', function (err) { console.error('Something went wrong on the client', err); }); ``` package/test/local-test.js000666 000000 000000 0000000644 13024533631014073 0ustar00000000 000000 var Client = require("../lib/node-rest-client.js").Client; var client = new Client(); console.log("client es ", client); /* client.get("http://localhost:8080/xml",(data,response) => console.log("DATA IS ", data)); */ client.addCustomHttpMethod("purge"); var request = client.purge("http://localhost:8080/xml",(data,response) => console.log("DATA IS ", data) ); console.log("REQUEST is ", request); package/test/specs.js000666 000000 000000 0000001741 13023770361013142 0ustar00000000 000000 var jasmine = require('jasmine-node'); var sys = require('sys'); for(var key in jasmine) { global[key] = jasmine[key]; } var isVerbose = true; var showColors = true; process.argv.forEach(function(arg){ switch(arg) { case '--color': showColors = true; break; case '--noColor': showColors = false; break; case '--verbose': isVerbose = true; break; } }); var options = {"specFolder":__dirname.concat("\\spec"), "onComplete": function(runner, log){ if (runner.results().failedCount == 0) { process.exit(0); } else { process.exit(1); } }, "isVerbose": isVerbose, "showColors": showColors}; jasmine.executeSpecsInFolder(options); package/test/test-proxy.js000666 000000 000000 0000004344 13023770361014165 0ustar00000000 000000 var http = require('http'); var sys = require('sys'); var fs = require('fs'); var blacklist = []; var iplist = []; fs.watchFile('./blacklist', function(c,p) { update_blacklist(); }); fs.watchFile('./iplist', function(c,p) { update_iplist(); }); function update_blacklist() { sys.log("Updating blacklist."); blacklist = fs.readFileSync('./blacklist').split('\n') .filter(function(rx) { return rx.length }) .map(function(rx) { return RegExp(rx) }); } function update_iplist() { sys.log("Updating iplist."); iplist = fs.readFileSync('./iplist').split('\n') .filter(function(rx) { return rx.length }); } function ip_allowed(ip) { for (i in iplist) { if (iplist[i] == ip) { return true; } } return false; } function host_allowed(host) { for (i in blacklist) { if (blacklist[i].test(host)) { return false; } } return true; } function deny(response, msg) { response.writeHead(401); response.write(msg); response.end(); } http.createServer(function(request, response) { var ip = request.connection.remoteAddress; if (!ip_allowed(ip)) { msg = "IP " + ip + " is not allowed to use this proxy"; deny(response, msg); sys.log(msg); return; } if (!host_allowed(request.url)) { msg = "Host " + request.url + " has been denied by proxy configuration"; deny(response, msg); sys.log(msg); return; } sys.log(ip + ": " + request.method + " " + request.url); var proxy = http.createClient(80, request.headers['host']); var proxy_request = proxy.request(request.method, request.url, request.headers); proxy_request.addListener('response', function(proxy_response) { proxy_response.addListener('data', function(chunk) { response.write(chunk, 'binary'); }); proxy_response.addListener('end', function() { response.end(); }); response.writeHead(proxy_response.statusCode, proxy_response.headers); }); request.addListener('data', function(chunk) { proxy_request.write(chunk, 'binary'); }); request.addListener('end', function() { proxy_request.end(); }); }).listen(8080); update_blacklist(); update_iplist();package/test/test-servers.js000666 000000 000000 0000004305 13023770361014472 0ustar00000000 000000 var http = require('http'), fs = require('fs'); // Create an HTTP server var httpSrv = http.createServer(function (req, res) { console.log("req.url", req.url); RouteManager.findRoute(req,res); }); var RouteManager ={ "findRoute":function(req,res){ var handler = this.routes[req.url]; if (!handler) throw "cannot find route " + req.url; handler.call(this,req,res); }, "routes":{ "/json":function(req,res){ //this.sleep(5000); var message = fs.readFileSync('./message.json','utf8'); res.writeHead(200, {'Content-Type': 'application/json'}); res.write(message.toString()); res.end(); }, "/xml":function(req,res){ var message = fs.readFileSync('./message.xml','utf8'); res.writeHead(200, {'Content-Type': 'application/xml'}); res.write(message.toString()); res.end(); }, "/120/json?arg1=hello&arg2=world":function(req,res){ if (!req.headers["test-header"]) throw "no test-header found!!"; res.setHeader("test-response-header",req.headers["test-header"]); this.routes["/json"](req,res); }, "/json?post":function(req,res){ req.on('data',function(data){ console.log("[SERVER] data = ", data); res.writeHead(200, {'Content-Type': 'application/json'}); //res.writeHead(200, {'Content-Type': 'text/plain'}); res.write(data.toString()); res.end(); }); }, "/json/empty":function(req,res){ res.writeHead(204, {'Content-Type': 'application/json'}); res.end(); }, "/xml/empty":function(req,res){ res.writeHead(204, {'Content-Type': 'application/xml'}); res.end(); }, "/json/contenttypewithspace":function(req,res){ var message = fs.readFileSync('./message.json','utf8'); res.writeHead(200, {'Content-Type': 'application/json; charset=utf-8'}); res.write(message.toString()); res.end(); } }, "sleep":function(ms){ var stop = new Date().getTime(); while(new Date().getTime() < stop + ms) { ; } } }; httpSrv.on('error',function(err){ console.error('error starting http test server',err); }); httpSrv.listen(4444); console.log('http server Listening on port ' + 4444); package/test/message.json000666 000000 000000 0000001621 13023770361014003 0ustar00000000 000000 {"catalog":{ "books":[{ "id":"bk101", "author":"Gambardella, Matthew", "title":"XML Developer's Guide", "genre":"Computer", "price":44.95, "publish_date":"2000-10-10", "description":"An in-depth look at creating applications with XML." }, { "id":"bk102", "author":"Gambardella, Matthew", "title":"JAVA Developer's Guide", "genre":"Computer", "price":188.95, "publish_date":"2000-10-10", "description":"An in-depth look at creating applications with JAVA." }, { "id":"bk103", "author":"Gambardella, Matthew", "title":"JSON Developer's Guide", "genre":"Computer", "price":422.95, "publish_date":"2000-10-10", "description":"An in-depth look at creating applications with JSON." } ] } } package/test/message.xml000666 000000 000000 0000003020 13023770361013625 0ustar00000000 000000 Gambardella, Matthew XML Developer's Guide Computer 44.95 2000-10-01 An in-depth look at creating applications with XML. Ralls, Kim Midnight Rain Fantasy 5.95 2000-12-16 A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world. Corets, Eva Maeve Ascendant Fantasy 5.95 2000-11-17 After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society. Corets, Eva Oberon's Legacy Fantasy 5.95 2001-03-10 In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant.