在官方文档中有这么一段示例代码
var postData = querystring.stringify({
'msg' : 'Hello World!'
});
var options = {
hostname: 'www.google.com',
port: 80,
path: '/upload',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
}
};
var req = http.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});
});
req.on('error', (e) => {
console.log(`problem with request: ${e.message}`);
});
// write data to request body
req.write(postData);
req.end();
由于我nodejs知识还是比较薄弱,所以现在还是想问大家一下 如果我想发送多个http请求,只是postData
的参数不一样,我该在哪里设置循环?是
var req = http.request(options, (res) => {
});
req.write(postData);
req.end();
还是应该
req.write(postData);
多次
req.write(postData);
算是一次请求。所以根据你的多个http请求应该是请求多项数据(我觉得接受请求的服务器应该是针对每次请求做响应,而不一定会去分析多个postData叠加的结构),应该是执行多次整体部分。贴个文档吧中文文档
上面也是我的理解。有错误欢迎指出。