http中原生的request不怎麼好用,有個第三方的request模塊挺好用的,簡單用了一下。
http get:git
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:github
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'); } });