需求:通過thinkjs的action,轉發下載指定url的文件。
簡單示例:
public async nuonuofpAction() {
const PassThrough = require('stream').PassThrough;
this.body = request.get('https://thinkjs.org/static/img/new/logo.png')
.on('response', (response) => {
let keys = Object.keys(response.headers);
for (let key of keys) {
this.ctx.set(key, response.headers[key]);
}
})
.on('error', this.ctx.onerror)
.pipe(PassThrough());
return;
}