简体   繁体   中英

why can i not convert this stream from process.stdout to a string

So i created this server:

    const { createServer} = require('http');
createServer(
    (request, response) => {
        response.writeHead(200, { "Content-Type": "text/plain" });

        request.on("data", chunk => {
            response.write(chunk.toString());
        });
        request.on("end", () => {
            response.end();
        });
    }
).listen(8000);

as well as this client:

    const {request} = require('http');
request({
    hostname:'localhost',
    port: 8000,
    method:'POST',

},response=>{
    response.on("data", chunk => process.stdout(chunk.toString()));

}).end("Hello Server");

Initially i had getaddrinfo ENOENT error but when it stopped when i got on wifi, now im getting this error:

 response.on("data", chunk => process.stdout(chunk.toString()));
                                         ^

TypeError: process.stdout is not a function`

any help folks

please try this one

var readline = require('readline');
readline.cursorTo(process.stdout, 0);
process.stdout.write(`waiting ... ${p}%`);

process.stdout is a Stream . To write to a specific stream, you should use its write() method (as long as it is a writeable stream). The stream object itself is not a function and can, hence, not be called like one.

response.on( 'data', chunk => process.stdout.write( chunk.toString() ) );

If your really want to pass on all content (as your current example suggests), you can also use the pipe() method to copy all output of a (readable) stream to a (writeable) stream:

response.pipe( process.stdout );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM