Nodejs HTTP request module written with request and Bluebird

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.