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');
}
});