node.js - 更改管道响应的内容处置

标签 node.js express

我有以下 Controller ,它从服务获取文件并将答案通过管道传递给浏览器。

function (req,res){
  request.get(serviceUrl).pipe(res);
}

我想更改内容配置(从附件到内联),以便浏览器打开文件而不是直接下载它。

我已经尝试过这个,但它不起作用:

function (req,res){
  res.set('content-disposition','inline');
  request.get(serviceUrl).pipe(res);
}

我使用的版本是:

  • NodeJS:0.12.x
  • express :4.x

最佳答案

为此,您可以在请求和响应之间使用中间 passtrhough 流,然后请求的 header 将不会传递到响应:

var through2 = require('through2'); // or whatever you like better

function (req, res) {
  var passThrough = through2(); // this stream is necessary to put correct response headers
  res.set('content-disposition','inline');
  request.get(serviceUrl).pipe(passThrough).pipe(res);
}

但要小心,因为这将忽略所有 header ,并且您可能需要指定“Content-Type”等。

关于node.js - 更改管道响应的内容处置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32725607/

相关文章:

javascript - EOL 未出现在 node.js 应用程序中

node.js - Mongoose 错误 : Arguments must be aggregate pipeline operators

javascript - Discord.js 类型错误 : Cannot read property 'id' of undefined

javascript - PostgreSQL 日期与我的查询休息一天

node.js - 通过 ajax 进行 Passport 身份验证

javascript - undefined 不是一个对象(评估 _this2.props.navigation.navigate)

node.js - 如何使用groupby获取分组并在mongoDB中获取动态状态的结果

node.js - 如何使用express/connect中的数据来猴子修补请求obj

node.js - 如何知道 express 仍在处理多少请求?

javascript - Mongoose 没有插入数组