var request =Require"Request");var promise =Require"Bluebird");Using Bluebird to compile all the async methods of the request library into promise form, the async suffix request = promise.promisifyall (request) is automatically added;var curl = (function() {functionCurl() { }/** * Enter a URL and data that needs to be post (if post is required), and then return the results of the final access. * *@author Jie *@date 2015/2/5 *@param the URL that the string URL needs to access. *@param string PostData Access, it is necessary to post past data. *@return Promise * * */Curl.visitasync =function(URL, postdata) {if (postdata = = = Void0) {PostData =""; }try {Request.debug = true;//Detailed Print request reportvar res; url = Url.trim ();To add an HTTP header for a URL that does not start with HTTP, you must addif (!url.match (/^http./)) {{URL ="http://" + URL; } }Default time-out timeout is 20 seconds without settingvar options = {url:url, form:"" };if (postdata) {Use the form in the request library to save the post data so that the accessed content-type becomes application/x-www-form-urlencoded options.form = PostData;Post Data res = Request.postasync (options);Returns the Promise function executed by the generation}else{res = request.getasync (options);} //returns the Promise function performed return res.spread ( function (res, body) {return body; }); } catch (ex) {throw Ex;}}; //visitasync return curl;}) (); module.exports = curl; using: test.jsimport curl = require ( ". Curl "); var tsk = Curl.visitasync (catch () can catch thrown errors, do not know the direct use of try-catch have no effect ~~~~~~~~
Https://www.npmjs.com/package/bluebird
Nodejs HTTP request module written with request and Bluebird