0

The first API request successfully send a response. However, when I do another GET request the error "write after end" is given.

When I turn off .pipe(addThing) then it does work on consecutive calls.

Is the through2-map function ending the connection or response somehow?

const fs = require('fs');
const express = require("express");
const route = express.Router();
const map = require("through2-map")
const csv2json = require("csv2json");
const firstThreeDigits = new RegExp(/[\s]*\d{3}/);

route.get("/", function(_, res){
  fs.createReadStream('./data/data.csv')
    .pipe(addThing)
    .pipe(csv2json({
      separator: ';'
    }))
    .pipe(res)
});

const addThing = map(function (chunk) {
  const chunkArr = chunk.toString().split('\n');
  const chunkWithNumber = chunkArr.map((line, index) => {
    if (index === 0) {
      return 'Number;' + line
    }
    return firstThreeDigits.exec(line) + ';' + line
  })
  return chunkWithNumber.toString().split(',').join("\n")
});

module.exports = route;

Not sure if it's relevant, but the csv:

./data/data.csv

Thing;Latitude;Longitude
FOO-123 Banana;52.09789;3.113278
BAR-456 Monocle;52.034599;5.11235
Remi
  • 4,663
  • 11
  • 49
  • 84

1 Answers1

0

After reading "Error: write after end" with csv-write-stream I noticed that the problem might be that the variable addThing is not created new on every consecutive call.

It was allocated in memory.

So the solution:

fs.createReadStream('./data/cameras-defb.csv')
    .pipe(map(addThing))
    .pipe(csv2json({
      separator: ';'
    }))
    .pipe(res);

function addThing(chunk) {
  const chunkArr = chunk.toString().split('\n');
  const chunkWithNumber = chunkArr.map((line, index) => {
    if (index === 0) {
      return 'Number;' + line
    }
    return firstThreeDigits.exec(line) + ';' + line
  })
  return chunkWithNumber.toString().split(',').join("\n")
})
Remi
  • 4,663
  • 11
  • 49
  • 84