Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    JavaScript

    Create simple POS with React.js, Node.js, and MongoDB #14: Export PDF, Excel, CSV, Bulk Delete, Inline Editing

    Consultation

    Best Background Check Services Assessments

    JavaScript

    Growth Hacking 101: Everything You Always Wanted to Know with Examples | 2019

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Saturday, September 27
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / Programming / 21. Node.js Lessons. Writable Response Stream (res), Pipe Method. Pt.1
    Programming

    21. Node.js Lessons. Writable Response Stream (res), Pipe Method. Pt.1

    Ivan RastvorovBy Ivan RastvorovOctober 27, 2016Updated:May 26, 2024No Comments7 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    21. Node.js Lessons. Writable Response Stream (res), Pipe Method. Pt.1
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link

    21-lesson_pt1

    Our next step will be using the streams to work with network connections. And we will start from delivering files to a visitor. As you may remember, we’ve already had a task like this: if a visitor requires the following url, you will give him the file. Let us create a file pipe.js with the following code (for your convenience, you can download code’ lesson from the repository because we’ll need an HTML file from there):

    var http = require('http');
    var fs = require('fs');
    
    new http.Server(function(req, res) {
      // res instanceof http.ServerResponse < stream.Writable
    
      if (req.url == '/big.html') {
    
        fs.readFile('big.html', function(err, content) {
          if (err) {
            res.statusCode = 500;
            res.end('Server error');
          } else {
            res.setHeader("Content-type", "text/html; charset=utf-8");
            res.end(content);
          }
        });
    
      }
    }).listen(3000);

    A solution example to this task without streams may be as follows, read the file:

    fs.readFile('big.html', function(err, content) {

    once the file is read, call callback:

     function(err, content) {
                if (err) {
                    res.statusCode = 500;
                    res.end('Server error');
                } else {
                    res.setHeader("Content-type", "text/html; charset=utf-8");
                    res.end(content);
                }
            });

    Next, if we’ve got an error, we send an alert, but if everything’s ok, we put a title specifying what file it is. And in response we write the file content as a call res.end(content)  that gives back the content and closes the connection. This solution generally works, but its problem is extended memory consumption because if a file is heavy,  readFile will first read it and then call a callback. As a result, if the client is slow, the whole read content will hang up in the memory, until the client gets it.

    But what if we’ve got a lot of slow clients of this kind? And what if a file is big? It turns out, a server can occupy the whole available memory in just a few seconds, which is not good for us. In order to avoid it, we will change the file return code to another one that uses streams. We can already read from the file using ReadStream:

    var file = new fs.ReadStream('big.html');

    It will be an input-data stream. While an output-data stream will be a response object res, which is an object of the http.ServerResponse  class that inherits from stream.Writable.

    The overall algorithm of using the streams for recording cardinally differs from those things that we’ve analyzed earlier. It looks like that.

    scheme

    First, we create a stream object. If we’ve got an http server, the object is already created. That is res. Next we want to send something to a client. We can do it by calling res.write and deliver our data there. It is generally a buffer or a string. Our data get added to a special stream parameter that is called a buffer. If this buffer is not that big yet, our data get added to it, and write returns true, which means we can write more. In this case, the stream itself becomes responsible for sending data. In general, the sending occurs asynchronously.

    Read More:  Node.js Experience

    Another variant is possible, too. For example, we’ve delivered too much data at a time or if a buffer is already occupied with something, the write method can return false, which means the internal stream buffer is overloaded and you can add your record at the moment, but it will be senseless because it all will be contained in the buffer. That’s why getting false оne does not continue recording waiting for a special event called drain that will be generated with a stream, when it sends everything and the internal buffer gets empty.

    Thus, we can call write multiple times, and whenever we realize all data are written down, we should call the end method. Here you can also transfer your data with the first argument. In this case, it will just call write . The most important task of end is to end the record. The stream does it and calls internal operations of closing resources (files), connections, etc., if needed. Then it generates the finish events, which means the record is finally ended.

    Note that a similar event at stream.writable is called end. This difference is not just a coincidence because there are duplex streams that can read and write. Respectively, they can generate both of these events.

    A stream can be destroyed at any time by calling the method destroy(). When calling this method the stream work gets ended, and all the resources associated to it get free. Of course, the finish event will never happen because this is already a successful ending for a stream work and successful delivery of all data.

    We provide the right file output by using the inquiry scheme as our crib sheet. Let us do it within a separate function named sendFile.
    It will get one stream for a file and the second one for its response.

    At first, pipe.js will look just like that:

    var http = require('http');
    var fs = require('fs');
    
    new http.Server(function(req, res) {
        // res instanceof http.ServerResponse < stream.Writable
    
        if (req.url == '/big.html') {
    
            var file = new fs.ReadStream('big.html');
            sendFile(file, res);
    
        }
    }).listen(3000);
    
    function sendFile(file, res) {
    
        file.on('readable', write);
    
        function write() {
            var fileContent = file.read();
    
            res.write(fileContent);
        }
    }

    The first thing we will do with such function is to wait for its data:

    function sendFile(file, res) {  
      
      file.on('readable', write); 
    

    Later, when all data is received, inside a handler readable , we will read them and send the following as the response:

    function write() {
            var fileContent = file.read();
    
            res.write(fileContent);
        }

    Of course, it doesn’t stand any criticism because if a client cannot receive these data (for instance, due to a slow Internet connection), they will be stuck within the object res buffer:

    res.write(fileContent)

    So, if a file is quickly read, but isn’t sent yet, it will occupy a lot of memory, and we would like to avoid a scenario of this kind. Within this short code we see an example of a versatile solution for this task. Copy this code part instead of the previous one:

    function sendFile(file, res) {
    
        file.on('readable', write);
    
        function write() {
            var fileContent = file.read(); // read
    
            if (fileContent && !res.write(fileContent)) { // send
    
                file.removeListener('readable', write);
    
                res.once('drain', function() { //wait
                    file.on('readable', write);
                    write();
                });
            }
        }
    
    
    }

    We also read the file content on the readable event, but we do not only send it with a call res.write, but also analyze what this call will return. If res accepts data very fast, then res.write will return true. It means, the branch if will never be performed

    if (fileContent && !res.write(fileContent)) {...}

    Respectively, we will get read-write,  read-write and so on.

    Read More:  23. Node.js Lessons. Domains, asynchronous try.. catch. Part 2.

    A more interesting occasion is when  res.write returns false. It means, when a buffer is overloaded, we temporarily reject handling readable events from the file.

    file.removeListener('readable', write);

    This stop of a handler does not mean the file stream will quit reading data. On the contrary, it will read the data, but will do it till a certain level, fill in its inside buffer of a file object, and then, as no one requests read, this internal buffer will stay loaded till a certain level. It means, a file stream will read up something and stop there. Next we will wait for a drain event.

    res.once('drain', function() {
                    file.on('readable', write);
                    write();
                });

    It means, whenever the data will be successfully delivered in response (it means, we can accept something else from the file), we demonstrate our interest in readable events again and immediately call the write method. Why? Just because when we’ve waited for this drain, new data can easily come. This means, you can immediately read them:

    var fileContent = file.read();
    

    The read call will return null, if you’ve got no data. Otherwise, they will be simply handled in the same way we’ve been talking about earlier in if.

    So, we get this kind of a recursive function: to read, send whatever has been read, waite for drain whether necessary, read again, send and wait – the same actions on loop until the file ends.

    tumblr_ng6oke7J1k1sxr61eo1_1280

    The article materials were borrowed from the following screencast.

    We are looking forward to meeting you on our website blog.soshace.com

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Ivan Rastvorov
    • Website

    Related Posts

    Mastering REST APIs: Essential Techniques for Programmers

    December 18, 2024

    Crafting Interactive User Interfaces Using JavaScript Techniques

    December 17, 2024

    Effective Strategies for Utilizing Frameworks in Web Development

    December 16, 2024
    Leave A Reply Cancel Reply

    You must be logged in to post a comment.

    Stay In Touch
    • Facebook
    • Twitter
    • Pinterest
    • Instagram
    • YouTube
    • Vimeo
    Don't Miss
    Trends April 17, 2019

    7 Job Recruitment Trends 2019 | From Self-Service Modules to Peculiarities of Gen Z

    Remote work, gig economy, Gen Z and AI: everything you need to know about recruitment trends.

    Strategic Approaches to Engaging Cold Prospects on LinkedIn

    November 29, 2024

    Error Handling in JavaScript: A Guide to Try/Catch, Throw, and Custom Error Classes

    March 30, 2023

    Роль Менеджера Продуктов

    June 1, 2016

    Categories

    • AI & Automation
    • Angular
    • ASP.NET
    • AWS
    • B2B Leads
    • Beginners
    • Blogs
    • Business Growth
    • Case Studies
    • Comics
    • Consultation
    • Content & Leadership
    • CSS
    • Development
    • Django
    • E-commerce & Retail
    • Entrepreneurs
    • Entrepreneurship
    • Events
    • Express.js
    • Facebook Ads
    • Finance & Fintech
    • Flask
    • Flutter
    • Franchising
    • Funnel Strategy
    • Git
    • GraphQL
    • Home Services Marketing
    • Influencer & Community
    • Interview
    • Java
    • Java Spring
    • JavaScript
    • Job
    • Laravel
    • Lead Generation
    • Legal & Compliance
    • LinkedIn
    • Machine Learning
    • Marketing Trends
    • Medical Marketing
    • MSP Lead Generation
    • MSP Marketing
    • NestJS
    • Next.js
    • Node.js
    • Node.js Lessons
    • Paid Advertising
    • PHP
    • Podcasts
    • POS Tutorial
    • Programming
    • Programming
    • Python
    • React
    • React Lessons
    • React Native
    • React Native Lessons
    • Recruitment
    • Remote Job
    • SaaS & Tech
    • SEO & Analytics
    • Soshace
    • Startups
    • Swarm Intelligence
    • Tips
    • Trends
    • Vue
    • Wiki
    • WordPress
    Top Posts

    The Future of Recruitment. Trends, Channels and Tools

    Trends October 2, 2018

    Ultimate Onboarding Checklist for Web Developers (Bonus: Onboarding Checklist for Freelancers)

    Remote Job July 15, 2019

    The Ultimate Guide to Using GitHub Pages

    Beginners May 10, 2019

    Node.js Lesson 12: HTTP Module and Nodemon

    Node.js December 11, 2020

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    About Us
    About Us

    Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

    7901 4th St N, Suite 28690
    Saint Petersburg, FL 33702-4305
    Phone: 1(877)SOSHACE

    Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
    Our Picks
    JavaScript

    React Lesson 15: Checking Homework progress from Lesson 14

    Programming

    2. Уроки Express.js . Логгер, Конфигурация, Шаблонизация с EJS. Часть 1.

    JavaScript

    Build Real-World React Native App #6: Show Bookmark and Categories

    Most Popular

    Effective Recruitment Strategies for Nonprofit Organizations

    Blogs

    Ensuring Quality: The Critical Role of Testing in Software Development

    Development

    Maximizing B2B Lead Generation with Interactive Content

    B2B Leads
    © 2025 Soshace Digital.
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions

    Type above and press Enter to search. Press Esc to cancel.