我想提供文件服务(使用NodeJS和HapiJS),而不保存到文件系统。
我有两个服务器。我需要通过server1从server2向客户端提供静态内容。因此,当客户端转到我路由时,我会在server1中调用server2,以便将文件保存在server1中,并使用它进行回复
我向服务器发出请求,如下所示:
var options = {
uri: 'http://192.168.11.101:8080' + req_path,
headers: {
Cookie: jCookie
}
};
var full_name = '/tmp/test.' + format;
var stream = httpRequest(options).pipe(fs.createWriteStream(full_name));此时文件已保存,我可以回复它。
我想直接回复而不保存到我的文件系统,可以吗?
发布于 2017-09-22 18:30:32
您可以使用下面的代码作为流发送,而不是保存file.Hope,这对您很有帮助。
let stream = fs.createReadStream(httpRequest(options));
stream.on('data', function(data)
{
console.log(data);
console.log('loaded part of the file');
//reply(null,data);
reply(data).header('Content-Type', 'file content type').header('Content-Disposition', 'attachment; filename=ex.txt');
});
stream.on('end', function()
{
console.log('file send sucessfully');
});https://stackoverflow.com/questions/36932155
复制相似问题