通过 Rest Builder 发送和接收文件

sending and recieving files through RestBulider

controller on localhost:8000
const fs = require("fs");
exports.install = function () {
  ROUTE("GET     /", indexPage);
};

function indexPage() {
  var self = this;

  console.log(" In GET ROUTE");


  RESTBuilder.GET("http://127.0.0.1:8500/getFile/").stream(function (
    err,
    response
  ) {
    if (err) {
      console.log(err);
      return;
    }
   
    var writer = fs.createWriteStream("./public/testBuilder.txt");
    // console.log("Writing to file");
    response.pipe(writer);
    self.json({ thankyou: "ok" });
  });
}


controller on localhost:8500

exports.install = function () {
  ROUTE("GET /getFile/", test);
};

function test() {
  var self = this;
  console.log("#################");
  console.log(self.body); 
  self.file("~trimSail/restBuilder.txt");
  // });
}

以上代码在 totaljs 3 中有效,但在 total4 中失败。 发送文件以响应 RestBuilder.GET 并将响应流式传输到文件。 错误 response.pipe 不是函数。

首先,请清理您的代码。

优化 Total.js:

const Fs = require('fs');

exports.install = function () {
    ROUTE('GET /', index);
};

function index() {

    var $ = this;

    console.log('In GET ROUTE');

    RESTBuilder.GET('http://127.0.0.1:8500/getFile/').stream($.successful(function(response) { 
        var writer = Fs.createWriteStream('./public/testBuilder.txt');
        response.stream.pipe(writer);
        $.json({ thankyou: 'ok' });
    }));

}