Vietnam

    Nodejs.vn

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Tags
    • Groups
    • Search

    Hỏi về stream.pipe và response.send

    Hỏi Đáp
    0
    1
    894
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      phatht last edited by phatht

      Xin chào các bạn, mình là newbie, mình đang phân vân khác nhau giữa stream.pipe và response.send, (thật tế là mình cũng như chưa thật sự hiểu stream để làm gì 😞 ) cụ thể là:
      Trong ứng dụng web bằng nodejs của mình, có dùng mustache để render template, và nó lại trả về stream:

      app.get('/', function(req, res){
       var tpl = '<h1>{{welcomeStr}}</h1>';
       var stream = mu.renderText(tpl, {welcomeStr: 'Hello World!'});
      	stream.pipe(res);
      });
      

      Mình cũng có thể chuyển từ stream sang string bằng cái này stream-to-string để send qua response:

      const stos = require('stream-to-string');
      app.get('/', function(req, res){
         var tpl = '<h1>{{welcomeStr}}</h1>';
         var stream = mu.renderText(tpl, {welcomeStr: 'Hello World!'});
         stos(stream, function(err, mes){
      					res.send(mes);
      					//res.end();
      				});
      });
      

      Việc chuyển này là do nhu cầu cần "format minimun" cái cục HTML sau khi render kia.
      Vậy mình dùng stream.pipe đối với dữ liệu html string bình thường có cần thiết không nhỉ cũng như tác dụng chính của stream.pipe là gì?

      1 Reply Last reply Reply Quote 0
      • First post
        Last post