简体   繁体   中英

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");
  // });
}

above code works in totaljs 3 but failing in total4. sending a file in response to RestBuilder.GET and streaming the response to file. error response.pipe is not a function.

First of all, please clean your code.

Optimized for 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' });
    }));

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM