изменение содержимого fs.createReadstream - PullRequest
0 голосов
/ 25 марта 2020

У меня есть требование, чтобы я читал файл по запросу express следующим образом:

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» читался по запросу, другими словами, как передать данные finalBuffer с res (ответ).

...