├── index.js ├── examples ├── download.js ├── getRequest.js ├── postRequest.js └── proxy.js ├── package.json ├── README.md └── lib └── nodegrass.js /index.js: -------------------------------------------------------------------------------- 1 | module.exports = process.env.NODEGLASS_COV 2 | ? require('./lib-cov/nodegrass') 3 | : require('./lib/nodegrass'); -------------------------------------------------------------------------------- /examples/download.js: -------------------------------------------------------------------------------- 1 | var nodegrass = require('../'); 2 | nodegrass.getFile('http://cdn.shopify.com/s/files/1/0051/4802/products/octodex_pack_preview_1024x1024.jpg?v=1327619907','d:/cccc/yep.jpg',function(e){ 3 | if(e){ 4 | console.log(e); 5 | } 6 | console.log('download success!'); 7 | }); -------------------------------------------------------------------------------- /examples/getRequest.js: -------------------------------------------------------------------------------- 1 | var nodegrass = require('../'); 2 | nodegrass.get("https://github.com",function(data,status,headers){ 3 | console.log(status); 4 | console.log(headers); 5 | console.log(data); 6 | },null,'utf8').on('error', function(e) { 7 | console.log("Got error: " + e.message); 8 | }); -------------------------------------------------------------------------------- /examples/postRequest.js: -------------------------------------------------------------------------------- 1 | var nodegrass = require('../'); 2 | nodegrass.post("http://127.0.0.1:8888/hello",function(data,status,headers){ 3 | console.log(status); 4 | console.log(headers); 5 | console.log(data); 6 | },null,{hello:"world"},'utf8').on('error', function(e) { 7 | console.log("Got error: " + e.message); 8 | }); -------------------------------------------------------------------------------- /examples/proxy.js: -------------------------------------------------------------------------------- 1 | var ng = require('../'), 2 | http=require('http'), 3 | url=require('url'); 4 | 5 | http.createServer(function(req,res){ 6 | var pathname = url.parse(req.url).pathname; 7 | 8 | if(pathname === '/'){ 9 | ng.get('http://stackoverflow.com/',function(data){ 10 | res.writeHeader(200,{'Content-Type':'text/html;charset=utf-8'}); 11 | res.write(data+"\n"); 12 | res.end(); 13 | },null,'utf8'); 14 | } 15 | }).listen(8088); 16 | console.log('server listening 8088...'); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "nodegrass", 3 | "version": "0.0.3", 4 | "description":"nodegrass is a easy tool to process client request for Node.js", 5 | "auther": "sk-scottkiss ", 6 | "contributors": [ "XadillaX " ], 7 | "keywords": [ 8 | "request", 9 | "nodegrass", 10 | "http", 11 | "https" 12 | ], 13 | "repository" : { 14 | "type": "git", 15 | "url": "git://github.com/scottkiss/nodegrass.git" 16 | }, 17 | "dependencies" : { 18 | "iconv-lite" : "0.2.5" 19 | }, 20 | "main" : "index", 21 | "engines" : { 22 | "node": ">= 0.4.1" 23 | }, 24 | "readme" : "# nodegrass.js - a tool for client request", 25 | "_id": "nodegrass@0.0.3", 26 | "license" : "MIT" 27 | } 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # What's nodegrass 2 | ====== 3 | 4 | nodegrass is a tool to process client request for Node.js 5 | 6 | ## Install 7 | ```bash 8 | $ npm install nodegrass 9 | ``` 10 | 11 | ## Useage 12 | ```js 13 | //get request 14 | var nodegrass = require('nodegrass'); 15 | nodegrass.get("https://github.com",function(data,status,headers){ 16 | console.log(status); 17 | console.log(headers); 18 | console.log(data); 19 | },null,'utf8').on('error', function(e) { 20 | console.log("Got error: " + e.message); 21 | }); 22 | 23 | //download file 24 | var nodegrass = require('nodegrass'); 25 | nodegrass.getFile('http://www.xxx.com/XXXX.jpg','d:/cccc/yep.jpg',function(e){ 26 | if(e){ 27 | console.log(e); 28 | } 29 | console.log('download success!'); 30 | }); 31 | 32 | ``` 33 | ## License 34 | 35 | (The MIT License) 36 | 37 | Copyright (c) 2011-2013 sk <skkmvp@hotmail.com> 38 | 39 | Permission is hereby granted, free of charge, to any person obtaining 40 | a copy of this software and associated documentation files (the 41 | 'Software'), to deal in the Software without restriction, including 42 | without limitation the rights to use, copy, modify, merge, publish, 43 | distribute, sublicense, and/or sell copies of the Software, and to 44 | permit persons to whom the Software is furnished to do so, subject to 45 | the following conditions: 46 | 47 | The above copyright notice and this permission notice shall be 48 | included in all copies or substantial portions of the Software. 49 | 50 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 51 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 52 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 53 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 54 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 55 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 56 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 57 | 58 | -------------------------------------------------------------------------------- /lib/nodegrass.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * Http-Https Request - nodegrass 3 | * Copyright(c) 2012 - 2013 Sk ScottKiss 4 | * MIT Licensed 5 | */ 6 | 7 | /** 8 | * Module dependencies. 9 | */ 10 | var http = require('http'), 11 | https = require('https'), 12 | querystring = require('querystring'), 13 | iconv = require('iconv-lite'), 14 | fs = require('fs'); 15 | 16 | 17 | 18 | function NodeGrass(){} 19 | 20 | NodeGrass.prototype._defaultHeaders = { 21 | 'Content-Type':'application/x-www-form-urlencoded', 22 | 'X-Powered-By':'nodegrass', 23 | 'User-Agent':'Mozilla/5.0 (Macintosh; Intel Cava OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11' 24 | } 25 | 26 | //download file 27 | NodeGrass.prototype.getFile = function(url,output,callback){ 28 | var header = {accept : '*/*'}; 29 | var options = { 30 | host:getHost(url), 31 | port:getPort(url), 32 | path:getPath(url), 33 | headers:header 34 | }; 35 | var content = ''; 36 | var protocol = getProtocol(url); 37 | protocol.get(options,function(res){ 38 | res.setEncoding('binary'); 39 | var status = res.statusCode; 40 | var headers = res.headers; 41 | res.on('data',function(chunk){ 42 | content += chunk; 43 | }); 44 | res.on('end',function(){ 45 | var resp = new Buffer(content,'binary'); 46 | fs.writeFile(output, resp, function(e) { 47 | if(typeof callback === 'function'){ 48 | callback(e); 49 | } 50 | 51 | }); 52 | }); 53 | }); 54 | 55 | } 56 | //Get Method Request 57 | //Support HTTP and HTTPS request,and Automatic recognition 58 | //@Param url 59 | //@Param callback 60 | NodeGrass.prototype.get = function(url,callback, reqheaders, charset){ 61 | var protocol = getProtocol(url); 62 | var _defaultCharSet = 'utf8'; 63 | 64 | if(typeof charset === 'string' ){ 65 | _defaultCharSet = charset; 66 | } 67 | if(typeof(reqheaders) === "string" && charset === undefined) { 68 | _defaultCharSet = reqheaders; 69 | } 70 | var newheader = {}; 71 | if(reqheaders !== undefined && typeof(reqheaders) === "object") { 72 | for(var ele in reqheaders) { 73 | newheader[ele.toLowerCase()] = reqheaders[ele]; 74 | } 75 | } 76 | newheader["content-length"] = 0; 77 | var options = { 78 | host:getHost(url), 79 | port:getPort(url), 80 | path:getPath(url), 81 | method:'GET', 82 | headers:newheader 83 | }; 84 | 85 | if(protocol === http || protocol === https){ 86 | return _sendReq(protocol,null,options,_defaultCharSet,callback); 87 | }else{ 88 | throw "sorry,this protocol do not support now"; 89 | } 90 | 91 | } 92 | 93 | //Post Method Request 94 | //Support HTTP and HTTPS request,and Automatic recognition 95 | //@Param url 96 | //@Param callback 97 | //@Param header 98 | //@param postdata 99 | NodeGrass.prototype.post = function(url,callback,reqheaders,data,charset){ 100 | var protocol = getProtocol(url); 101 | var _defaultCharSet = 'utf8'; 102 | 103 | if(typeof charset === 'string' ){ 104 | _defaultCharSet = charset; 105 | } 106 | 107 | if(typeof(data) === 'object'){data = querystring.stringify(data);} 108 | var options={ 109 | host:getHost(url), 110 | port:getPort(url), 111 | path:getPath(url), 112 | method:'POST', 113 | headers:reqheaders||{} 114 | }; 115 | options.agent = false; 116 | options.headers["Content-Length"] = options.headers["Content-Length"] || data.length; 117 | options.headers["X-Powered-By"] = options.headers["X-Powered-By"] || this._defaultHeaders["X-Powered-By"]; 118 | options.headers["User-Agent"] = options.headers["User-Agent"] || this._defaultHeaders["User-Agent"]; 119 | options.headers["Content-Type"] = options.headers["Content-Type"] || this._defaultHeaders["Content-Type"]; 120 | if(protocol === http || protocol === https){ 121 | return _sendReq(protocol,data,options,_defaultCharSet,callback) 122 | }else{ 123 | throw "sorry,this protocol do not support now"; 124 | } 125 | } 126 | 127 | //Parse the url,get the path 128 | //e.g. http://www.google.com/path/another -> /path/another 129 | function getPath(url){ 130 | var pathPattern = /\w+:\/\/([^\/]+)(\/.+)(\/$)?/i; 131 | var fullPath = url.match(pathPattern); 132 | return fullPath?fullPath[2]:'/'; 133 | } 134 | 135 | 136 | function _sendReq(protocol,data,options,_defaultCharSet,callback){ 137 | var content = ""; 138 | var req = protocol.request(options,function(res){ 139 | var status = res.statusCode; 140 | var headers = res.headers; 141 | if(_defaultCharSet==="gbk"){ 142 | res.setEncoding('binary'); 143 | }else{ 144 | res.setEncoding(_defaultCharSet); 145 | } 146 | res.on('data',function(chunk){ 147 | content+=chunk; 148 | }); 149 | res.on('end',function(){ 150 | if(_defaultCharSet==="gbk"){ 151 | content = iconv.decode(new Buffer(content,'binary'),'gbk'); 152 | } 153 | callback(content,status,headers); 154 | }); 155 | }); 156 | if(null != data){ 157 | req.write(data+"\n"); 158 | } 159 | req.end(); 160 | return req; 161 | } 162 | 163 | //Parse the url, get the port 164 | //e.g. http://www.google.com/path/another -> 80 165 | // http://foo.bar:8081/a/b -> 8081 166 | function getPort(url) { 167 | var hostPattern = /\w+:\/\/([^\/]+)(\/)?/i; 168 | var domain = url.match(hostPattern); 169 | 170 | var pos = domain[1].indexOf(":"); 171 | if(pos !== -1) { 172 | domain[1] = domain[1].substr(pos + 1); 173 | return parseInt(domain[1]); 174 | } else if(url.toLowerCase().substr(0, 5) === "https") return 443; 175 | else return 80; 176 | } 177 | 178 | //Parse the url,get the host name 179 | //e.g. http://www.google.com/path/another -> www.google.com 180 | function getHost(url){ 181 | var hostPattern = /\w+:\/\/([^\/]+)(\/)?/i; 182 | var domain = url.match(hostPattern); 183 | 184 | var pos = domain[1].indexOf(":"); 185 | if(pos !== -1) { 186 | domain[1] = domain[1].substring(0, pos); 187 | } 188 | return domain[1]; 189 | } 190 | 191 | //Get the Protocol 192 | //http://www.google.com/path/another => http 193 | function getProtocol(url){ 194 | return url.substring(0,url.indexOf(":")) === 'https' ? https : http;; 195 | } 196 | var nodegrass = new NodeGrass(); 197 | module.exports = nodegrass; 198 | --------------------------------------------------------------------------------