readline:文件的所有内容在一行
Readline: all the contents of the file in one line
这是我的问题。我正在使用路由发送文件以丰富(请参阅 router.js 文件)
这条路线将调用一个函数,该函数将负责逐行丰富文件。除了这里的问题,它不能逐行读取它。它在一行中显示文件的所有内容! (我放了一个console.log来验证)
我和我的同事找不到解决方案。我们犹豫要不要使用另一个图书馆。也许有人可以给我一个解决这个问题的方法。
提前致谢。
PS:我特意简化了代码来找出问题所在
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 --data "@/path/to/file/test.txt" localhost/enrich/json
代码不错,curl请求错误。
-F 参数更易于使用。
curl -F "file=@/path/to/file/test.txt" localhost/enrich/json
这项工作。
这是我的问题。我正在使用路由发送文件以丰富(请参阅 router.js 文件) 这条路线将调用一个函数,该函数将负责逐行丰富文件。除了这里的问题,它不能逐行读取它。它在一行中显示文件的所有内容! (我放了一个console.log来验证) 我和我的同事找不到解决方案。我们犹豫要不要使用另一个图书馆。也许有人可以给我一个解决这个问题的方法。
提前致谢。
PS:我特意简化了代码来找出问题所在
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 --data "@/path/to/file/test.txt" localhost/enrich/json
代码不错,curl请求错误。 -F 参数更易于使用。
curl -F "file=@/path/to/file/test.txt" localhost/enrich/json
这项工作。