【问题标题】:Readline: all the contents of the file in one linereadline:将文件的所有内容放在一行中
【发布时间】:2021-02-10 16:31:55
【问题描述】:

这是我的问题。我正在使用路由发送文件以丰富(请参阅 router.js 文件) 该路由将调用一个函数,该函数将逐行丰富文件。除了这里的问题,它不能逐行读取。它在一行中显示文件的所有内容! (我放了一个console.log来验证) 我和我的同事找不到解决办法。我们犹豫是否要使用另一个库。也许有人可以给我一个解决这个问题的方法。

提前谢谢你。

PS:我特意简化代码来识别问题。

文件.txt

examples
examples
examples
examples
examples
examples
examples
examples
examples
examples

路由器.js

const router = require('express').Router();
const { enrichmentFileJSON } = require('../services/enrich/json');

router.post('/enrich/json', async (req, res) => {
  let { args } = req.query;
  args = 'is_oa';
  const enrichedFile = await enrichmentFileJSON(req, args);
});

module.exports = router;

json.js

const readline = require('readline');

/**
 * starts the enrichment process for files JSON
 * @param readStream read the stream of the file you want to enrich
 * @param args attributes will be add
 */
const enrichmentFileJSON = async (readStream, args) => {
  const rl = readline.createInterface({
    input: readStream,
    crlfDelay: Infinity,
  });
  for await (const line of rl) {
    console.log('==============');
    console.log(line);
    console.log('==============');
    // this print me :
    // ==============
    //examplesexamplesexamplesexamplesexamplesexamplesexamplesexamplesexamplesexamples
    // ==============
  }
});

module.exports = {
  enrichmentFileJSON,
};

卷曲请求

curl --data "@/path/to/file/test.txt" localhost/enrich/json

【问题讨论】:

  • 您是否在使用其他快速中间件或正文解析器?
  • 我没有在这个路由器上使用中间件。除了cors

标签: node.js readline


【解决方案1】:

代码是好的,curl 请求是错误的。 -F 参数更易于使用。

curl -F "file=@/path/to/file/test.txt" localhost/enrich/json

这项工作。

【讨论】:

    猜你喜欢
    • 2019-08-22
    • 1970-01-01
    • 2020-07-25
    • 2012-05-18
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多