Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js “;在结束后写入”;使用through2映射使用NodeJ的服务器出错_Node.js_Express_Through2 - Fatal编程技术网

Node.js “;在结束后写入”;使用through2映射使用NodeJ的服务器出错

Node.js “;在结束后写入”;使用through2映射使用NodeJ的服务器出错,node.js,express,through2,Node.js,Express,Through2,第一个API请求成功发送响应。但是,当我执行另一个GET请求时,会出现“结束后写入”错误 当我关闭.pipe(addThing)时,它会对连续调用起作用 through2 map函数是否以某种方式结束了连接或响应 const fs=require('fs'); const express=要求(“express”); const route=express.Router(); const map=require(“through2 map”) const csv2json=require(“cs

第一个API请求成功发送响应。但是,当我执行另一个GET请求时,会出现“结束后写入”错误

当我关闭
.pipe(addThing)
时,它会对连续调用起作用

through2 map函数是否以某种方式结束了连接或响应

const fs=require('fs');
const express=要求(“express”);
const route=express.Router();
const map=require(“through2 map”)
const csv2json=require(“csv2json”);
const firstThreeDigits=new RegExp(/[\s]*\d{3}/);
route.get(“/”,函数(\uRes){
fs.createReadStream(“./data/data.csv”)
.pipe(addThing)
.pipe(csv2json({
分隔符:';'
}))
.管道(res)
});
const addThing=map(函数(块){
const chunkArr=chunk.toString().split('\n');
const chunkWithNumber=chunkArr.map((行,索引)=>{
如果(索引==0){
返回'Number;'+行
}
返回前三位数字。exec(行)+';'+行
})
返回chunkWithNumber.toString().split(',').join(\n)
});
module.exports=路线;
不确定是否相关,但csv:

/data/data.csv

Thing;Latitude;Longitude
FOO-123 Banana;52.09789;3.113278
BAR-456 Monocle;52.034599;5.11235
阅读之后,我注意到问题可能在于变量
addThing
不是在每次连续调用时都创建新的

它是在内存中分配的

因此,解决方案是:

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")
})