Search code examples
node.jsreadline

Readline: all the contents of the file in one line


here is my problem. I am using a route to send a file to enrich (see the router.js file) This route will call a function which will take care of enriching the file line by line. Except here's the problem, It can't read it line by line. It displays all the contents of the file in one line! (I put a console.log to verify) Me and my colleagues cannot find a solution. We hesitate to use another library. Maybe someone can give me a solution to this problem.

thank you in advance.

PS: I intentionally simplify the code to identify the problem.

file.txt

examples
examples
examples
examples
examples
examples
examples
examples
examples
examples

router.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 request

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


Solution

  • the code is good, the curl request is wrong. the -F argument is easier to use.

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

    this work.