GET请求:
var req = http.request(options,function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data',function (chunk) {
console.log('BODY: ' + chunk);
});
});
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data',function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error',function (e) {
console.log('problem with request: ' + e.message);
});
req.end();
POST请求:
var req = http.request(options,function (e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write(content);
req.end();
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write(content);
req.end();
以上这篇node作为中间服务层如何发送请求(发送请求的实现方法详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。
原文链接:https://www.f2er.com/nodejs/34389.html