sending and recieving files through RestBulider

71 views Asked by At
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.

1

There are 1 answers

0
Peter Sirka On BEST ANSWER

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

}