Welcome to the Linux Foundation Forum!

Lab 4.2 - Stream Some Content

Hi there,

app.js file:

  1. const express = require('express');
  2. const app = express();
  3.  
  4. const stream = require('./stream')
  5.  
  6. app.get('/data', (req,res) => {
  7. res.type('text/html')
  8. res.send(stream());
  9. })
  10.  
  11. app.listen(3000);

node validate fails with the following:

  1. response data should be "this<br>is<br>a<br>stream<br>of<br>data<br>"
  2.  

When I visit localhost:3000/data this is the response:

{"_readableState":{"objectMode":false,"highWaterMark":16384,"buffer":{"head":null,"tail":null,"length":0},"length":0,"pipes":.........

Any idea what is wrong?

TIA

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Answers

  • Ok this seems to work:

    1. app.get('/data', (req, res) => {
    2. res.type('text/html');
    3. const readableStream = stream();
    4. readableStream.pipe(res);
    5. });
    1. function(req, res, next) {
    2. res.type('text/html');
    3. const readable = stream()
    4. readable.pipe(res, {end: false});
    5. finished(readable, (err) => {
    6. if (err) {
    7. next(err);
    8. return;
    9. }
    10. res.end();
    11. })
    12. }

    This could be another approach. The key is to invoke the stream() function only in the request handler method.

  • Posts: 160

    exactly, in Express you have to set up the stream piping. You can also use require('stream').pipeline(stream(), res, next) - the next function is automatically called (with any error) when the pipe finishes

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training