node.jsbuffernodejs-server

Use case of buffer in nodejs


How to fully use buffer in projects and system. how can you take advantage of buffer in creating your project. tried looking at the documentation and articles but it doesn't show any use case regarding buffer on live projects.


Solution

  • Buffers in Javascript are generally for binary data or data that hasn't been decoded yet.

    If your data is a string or a number, you just use those native types.

    For example, if you wanted to read an image from disk and examine the EXIF data in the image, you would load at least the first part of the image into a buffer and then parse the binary data in the buffer to interpret the EXIF data.

    Or, if you were writing code to decompress a file from scratch, you'd load a piece of the file into a buffer and run your decompression code on the binary data in the buffer.

    Note, this is generally lower level programming (dealing with binary data) and there may be many projects that do not need to do that kind of manipulation themselves as you can often rely on already written libraries to do things like compression or decompression or encoding or decoding.

    For example, if you use a library such as got() or axios() to make http requests, then those libraries will automatically handle the GZIP compression format for http responses so you don't have to handle the decompression yourself.