创建一个简单的client

var http = require('http');

var opt = {
    host: 'www.baidu.com',
    port: '80',
    method: 'GET',
    path: '/',
    hearders: {
        'Content-Type': 'application/json'
    }
};

var body = '';
var req = http.request(opt, function (res) {
    console.log('Got response: ' + res.statusCode);
    res.on('data', function (data) {
        body += data;
    });
    res.on('end', function () {
        console.log(res.headers);
        console.log(body);
    });
});

req.on('error', function (err) {
    console.log('Got error: ' + err.message);
});

req.write('');
req.end();

推荐阅读更多精彩内容