更改 fs.createReadstream 的内容
changing content of fs.createReadstream
我有这样的要求,我正在阅读一个明确请求的文件,如下所示:
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(response) 传输 finalBuffer 数据。
终于找到了使用节点js流从缓冲区创建读取流的方法。
我从 here 得到了精确的解决方案。
我刚刚放了一些代码,比如
const fs = require('fs');
const os = require('os');
const path = require('path');
var express = require("express");
app = express();
app.get('/getdata', function (req, res) { // getdata means getting decrypted data
fs.readFile(file_location, function read(err, data) {
// here I am performing my decryption logic which gives output say
//"decryptedBuffer"
var stream = require("./index.js");
stream.createReadStream(decryptedBuffer).pipe(res);
})
})
// index.js
'use strict';
var util = require('util');
var stream = require('stream');
module.exports.createReadStream = function (object, options) {
return new MultiStream (object, options);
};
var MultiStream = function (object, options) {
if (object instanceof Buffer || typeof object === 'string') {
options = options || {};
stream.Readable.call(this, {
highWaterMark: options.highWaterMark,
encoding: options.encoding
});
} else {
stream.Readable.call(this, { objectMode: true });
}
this._object = object;
};
util.inherits(MultiStream, stream.Readable);
MultiStream.prototype._read = function () {
this.push(this._object);
this._object = null;
};
如果有人对此有任何疑问,请发表评论,我会尽力让 him/her 理解我的代码片段。
我有这样的要求,我正在阅读一个明确请求的文件,如下所示:
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(response) 传输 finalBuffer 数据。
终于找到了使用节点js流从缓冲区创建读取流的方法。 我从 here 得到了精确的解决方案。 我刚刚放了一些代码,比如
const fs = require('fs');
const os = require('os');
const path = require('path');
var express = require("express");
app = express();
app.get('/getdata', function (req, res) { // getdata means getting decrypted data
fs.readFile(file_location, function read(err, data) {
// here I am performing my decryption logic which gives output say
//"decryptedBuffer"
var stream = require("./index.js");
stream.createReadStream(decryptedBuffer).pipe(res);
})
})
// index.js
'use strict';
var util = require('util');
var stream = require('stream');
module.exports.createReadStream = function (object, options) {
return new MultiStream (object, options);
};
var MultiStream = function (object, options) {
if (object instanceof Buffer || typeof object === 'string') {
options = options || {};
stream.Readable.call(this, {
highWaterMark: options.highWaterMark,
encoding: options.encoding
});
} else {
stream.Readable.call(this, { objectMode: true });
}
this._object = object;
};
util.inherits(MultiStream, stream.Readable);
MultiStream.prototype._read = function () {
this.push(this._object);
this._object = null;
};
如果有人对此有任何疑问,请发表评论,我会尽力让 him/her 理解我的代码片段。