npm install request

http中原生的request不怎么好用,有个第三方的request模块挺好用的,简单用了一下。
http get:


var request = require('request');
request.get({url: 'http://t.co', timeout: 3000}, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body);
  } else {
    console.log('not success');
  }
});

http post:


request.post({url: 'http://localhost:3000/', timeout: 3000, form: {key1: 'value2', key2: 'value2'}}, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body);
  } else {
    console.log('not success');
  }
});
    原文作者:orz71
    原文地址: https://segmentfault.com/a/1190000000516641
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞