【发布时间】: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