- get请求
var http = require(‘http‘); var opts = { host: ‘www.google.com‘ port: 80, path: ‘/‘, }; var req = http.get(opts, function(res) { console.log(res); res.setEncoding(‘utf8‘); //设置编码后才能正常显示. res.on(‘data‘, function(data) { console.log(data); }); });
- 普通请求
var options = { host: ‘www.example.com‘, port: 80, path: ‘/submit‘, method: ‘POST‘ }; var req = http.request(options, function(res) { res.setEncoding(‘utf8‘); res.on(‘data‘, function (chunk) { console.log(‘BODY: ‘ + chunk); }); }); req.write("my data"); //每一次write调用都会立马上传数据,不会缓存. req.write("more of my data"); //可以把流的data事件绑定到write方法达到写流的目的. req.end(); //end方法调用之前,服务器不会响应.
时间: 2024-10-12 08:06:42