Node.js: 遍历 TXT 文件并使用 Axios 发送多个请求
您可以使用 Node.js 内置的 readline 模块遍历 txt 文件的每一行,并且在每一行中获取数据,并使用 axios 发送多个请求。
以下是一个示例代码:
const readline = require('readline');
const axios = require('axios');
const rl = readline.createInterface({
input: fs.createReadStream('file.txt'),
crlfDelay: Infinity
});
rl.on('line', (line) => {
// 在每一行中获取数据
const data = line.split(',');
// 发送 axios 请求
axios.post('http://example.com/api', data)
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
});
在这个示例中,我们使用 readline 模块创建了一个接口,然后使用 on 方法监听每一行数据,并在每一行中获取数据。然后,我们使用 axios 发送 POST 请求,在请求成功时输出响应数据,否则输出错误信息。
原文地址: https://www.cveoy.top/t/topic/ndkJ 著作权归作者所有。请勿转载和采集!