您可以使用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请求,在请求成功时输出响应数据,否则输出错误信息。

nodejs遍历txt每一行获取数据使用axios发送多个请求

原文地址: https://www.cveoy.top/t/topic/bLjL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录