Welcome to the Linux Foundation Forum!

Ch. 12 Piping Streams Client/Server not working for me?

Options

Hello,

I'm doing the LFW211 Node.JS Applications Certification course, and I'm on Chapter 12 about Streams. In the last section on Piping Streams, the course re-writes the previous Duplex Streams Client/Server example. I tried copying this code into two different files, a server-piping.js file and a client-piping.js file. I've tried running this, but it never works the way you show in that Chapter 12 on Streams.

First, my set-up — I'm on a 2022 MacBook Air with the Apple Silicon M2 chip, 24 GB of RAM and MacOS Sonoma v14.2.1. I'm running Node.JS v20.11.0 on this MacBook.

In your example, you say the result of running the server and then the client should be —

Desired Client-Server Streams Piping Result picture

Here's my server-piping.js file —

'use strict'

const net = require('net')
const { pipeline } = require('stream')
const { createTransformStream } = require('./client-piping.js')

net.createServer((socket) => {
    const transform = createTransformStream()
    const interval = setInterval(() => {
        socket.write('beat')
    }, 1000)
    pipeline(socket, transform, socket, (err) => {
        if (err) {
            console.error('There was a socket error closing the pipelin: ', err)
        }
        clearInterval(interval)
    })
}).listen(3000)

and here's my client-piping.js file —

'use strict'

const { Transform } = require('stream')
const { scrypt } = require('crypto')
function createTransformStream() {
    return new Transform({
        decodeStrings: false,
        encoding: 'hex',
        transform(chunk, enc, next) {
            scrypt(chunk, 'a-salt', 32, (err, key) => {
                if (err) {
                    next(err)
                    return
                }
                next(null, key)
            })
        }
    })
}

module.exports = createTransformStream

When I run the server and then the client files, it just sits there for many minutes, up to 10 minutes, then I get the following —

 $ node server-piping.js 
/Users/faddah/Documents/code/learning/LinuxFound-OpenJS/NodeJSApps/labs-dec-2023/ch-12/streams/server-piping.js:25
        const transform = createTransformStream()
                          ^

ReferenceError: createTransformStream is not defined
    at Server.<anonymous> (/Users/faddah/Documents/code/learning/LinuxFound-OpenJS/NodeJSApps/labs-dec-2023/ch-12/streams/server-piping.js:25:20)
    at Server.emit (node:events:518:28)
    at TCP.onconnection (node:net:2163:8)

Node.js v20.11.0

i get this, event though i've done a module.exports = createTransformStream" in theclient-piping.jsfile and imported it in the top of theserver-piping.js` file. I would just like to see this code fun as it says it should run in the chapter.

Also, I have now asked about three or four questions in this forum in the last few weeks, and absolutely NONE of them have been answered by anyone. I understand already that this is a Community forum and Linux Foundation / OpenJS folk or the instructor are not often on here nor are required to answer, and it's a Community forum with answered supplied by the User Community; however, I find it really strange that I've posed these questions with absolutely no response, but others in this forum have had their questions / discussion readily answered since then. Could I please get someone to look at this question and the previous quesitons I've posed here on this forum and get some answers, please?

best,

faddah

Answers

  • xdxmxc
    xdxmxc Posts: 148
    edited April 1
    Options

    @faddah it's to do with your module.export - you export the function but you import a property on that function-object - which doesn't exist

Categories

Upcoming Training