P
P
pegas2018-08-13 10:22:04
JavaScript
pegas, 2018-08-13 10:22:04

How are streams rendered in node.js?

app.get('/stream', function(req, res) {
  var stream = fs.createReadStream(__dirname + 'text.txt')
  stream.pipe(res)
})

appa .get('/stream', function(req, res) {
var stream = fs.createReadStream(__dirname + 'text.txt') stream.pipe
(res)
} ) we pass data to res, but how is this data displayed in the browser gradually? After all, usually res takes data (entirely as is) and they are displayed in the browser (entirely). Or an example with a picture:
app.get('/image', function(req, res) {
  var stream = fs.createReadStream(__dirname + 'image.png')
  stream.pipe(res)
})

How does she twirl in pieces?
Or .js code, for example, if only one part of the code is loaded, and the second part of which it depends on the first one, has not yet loaded, and when executing this .js in the browser, there will be an error.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey P, 2018-08-14
@1PeGaS

If you look at the vorpos a little wider, you can stumble upon information about MIME-types and server response headers. In short, not all formats support "streaming" data transfer. Basically, some video formats (MP4, MOV, etc.) and some audio formats (AAC, etc.) support this type. Pictures are usually transferred as a whole (as well as pdf), if there is no other implementation on the server.
These were general words, now on the issue. Thread, in nodejs, is an abstraction i.e. it does not mean that the data is being streamed. Behind the scene of `stream.pipe` is working with `data`, `end` and other events . Those. we get the stream, subscribe to `data` (read the data) and `end` (close the stream). A clearer example can be found here . It's better to read this book .where everything is detailed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question