【发布时间】:2020-03-25 05:49:41
【问题描述】:
我有这样的要求,即我正在阅读以下明确要求的文件:
const fs = require('fs');
const os = require('os');
const path = require('path');
var express = require("express");
app = express();
app.get('/getdata', function (req, res) {
var stream = fs.createReadStream('myFileLocation');// this location contains encrypted file
let tempVariable = [];
stream.on('data', function(chunk) {
tempVariable += chunk;
});
stream.on('end', function () {
*****here I read tempVariable and using it I decrypt the file content and output a buffer (say,finalBuffer)****
})
stream.on('error', function (error) {
res.writeHead(404, 'Not Found');
res.end();
});
stream.pipe(res);
那么我应该怎么做才能使“finalBuffer”在请求时可读,换句话说,如何使用 res(响应)来传递 finalBuffer 数据。
【问题讨论】:
-
如果您想使用流,那么您将创建一个流转换,以增量方式解密数据。这将允许您将其通过管道传递给响应。按照您现在的方式,当您到达
end事件时,内容已经发送。这是good article。
标签: node.js express pipe filestream