Code Monkey home page Code Monkey logo

node-js-streams's Introduction

NodeJS Streams

codecov

Basic example using NodeJS Streams.

Installation

Git clone this repository:

git clone https://github.com/Guilospanck/node-js-streams.git

"Slow" test

Change directory into the repository:

cd node-js-streams/

Then run:

yarn

To install dependencies and then run:

yarn start:dev

Fast test

Be sure to have Docker and Docker Compose installed.

Change directory into the repository:

cd node-js-streams/

Then run:

sudo docker-compose -f docker-compose.yml up -d --build

In order to view the logs once it's built, run:

sudo docker ps # get the id of the container
sudo docker logs [id]

Streams

Basically we have three main structures: Readable, Transform and Writable.

Readable

The Readable is the source of data. So, let's say we have a client-server architecture and we, the client, want to retrieve some information from the server.

The Readable will be kept in the server and it will read and send the stream of data.

Transform

The Transform is the man in the middle. It's responsible for making changes on the data. Everything you want to do with the stream of data, you can use the Transform to do that.

Writable

The Writable usually lies in the client and is responsive to finally do something with the data received.

Chunk and callbacks

  • Chunk is the data coming from the stream. It is a buffer and if you want to get the data, just parse it using JSON.parse(chunk); or use toString();.
  • Callback is used to inform the server (the readable stream) that the client received the data and it can send the other. If we don't pass it, it won't get the next data.

TypeScript

Install TS dev dependencies:

yarn add -D typescript ts-node

Then generate a new tsconfig.json file:

npx tsc --init

Use the tsconfig.json that exists in this repository for a more complete version.

Also, add a new file called nodemon.json in order to run Nodemon with TypeScript without getting an error.


Jest with TS

Install the Jest dev dependencies:

yarn add -D jest ts-jest @types/jest

Then generate a new jest.config.js (or jest.config.cjs) file:

npx ts-jest config:init

node-js-streams's People

Contributors

guilospanck avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.