What does process stdout connect do? - node.js

What does
process.stdout.connect({})
do and how can I use it?
Tryied googleing it but found nothing.
Tryied options but get error:
TypeError: self._handle.connect is not a function

As the documentation says, process.stdout returns a stream connected to stdout and that stream is of type Socket. Now, if you check the documentation for Socket and look for the connect method you'll find that it initiates a connection on a given socket.
In order to use process.stdout you don't actually need to call connect, you can just write arbitrary strings to the stream which will get echoed in your stdout, e.g.
process.stdout.write("test"); // will print test to the console
But if logging to the console is your intent, it's probably easier to just use the provided console.log() which adds a lot of formatting and other stuff (see this for further info).

Related

Reading cli in Nodejs explanation required

process.stdin.on('data',function(data){input_std_in+=data});
What is the correct explanation for the above piece of code. I am new to Node.js and have found many variations of this on the net, but I am still not clear.
The process.stdin is used to read data from commandline (Simple Explaination) For more see here
So below you are waiting for data event of stdin ie you wait for user to type some data in terminal ,you read it and append it to some string.As node js (javascript) is event driven ,it waits for some event to happen get the data from that event and use it further ,like in the script below it appends to already declared variable.
let input_std_in="";
process.stdin.on('data',function(data){
console.log("Data",data.toString())
input_std_in+=data.toString()
});
See working here

Process Object Stdout and StdIn do not work as expected

I am using vsCode for NodeJS.
My code was simple. So I was expecting stdout should wait for input from console. Once input is given, then it should go to exit.
But once i do > node process
the program goes to exit directly.
file - process.js ->
process.stdout.write('Ask me something : ');
process.stdin.on('data', function(answer){
console.log(answer.toString());
});
console.log('exit')
process.exit();
Output:
c:\node-projects\demo-nodejs>node process
Ask me something : exit
c:\node-projects\demo-nodejs>
How can I use process object to get standard input/output/print/exit the code instead of using other readline object?
There are a few question/answers such as this one already on this site that probably answer what you are asking. Your code is not working because the line with process.stdin.on is not a synchronous callback method call, it is an event handler that waits for the 'data' event.
If I understand what you are trying to do, I think it is easiest to import the 'readline' module. It is built-in to the nodejs executable, but not loaded to your code execution environment unless you require it.
That would make your code look like this:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Ask me something : ', function(answer){
console.log(answer.toString());
console.log('exit')
process.exit();
});
Note that the rl.question method call executes right away and calls the function(answer) function when the user is done with text input. It can take some understanding and getting used to the asynchronous program control flow in NodeJS, you might have to rethink some of the rest of your program structure.
The offical documentation for the readline NodeJS module and function is here.

how to save an image from discord using discord bot node js

So far I've only been able to get text and links from what other people on my discord channel type, but I want to be able to save posted images/gifs. is there any way I can do this through the bot or is it impossible? I'm using discord.js.
Images in Discord.js come in the form of MessageAttachments via Message#attachments. By looping through the amount of attachments, we can retrieve the raw file via MessageAttachment#attachment and the file type using MessageAttachment#name. Then, we use node's FileSystem to write the file onto the system. Here's a quick example. This example assumes you already have the message event and the message variable.
const fs = require('fs');
msg.attachments.forEach(a => {
fs.writeFileSync(`./${a.name}`, a.file); // Write the file to the system synchronously.
});
Please note that in a real world scenario you should surround the synchronous function with a try/catch statement, for errors.
Also note that, according to the docs, the attachment can be a stream. I have yet to have this happen in the real world, but if it does it might be worth checking if a is typeof Stream, and then using fs.createWriteStream and piping the file into it.

What kind of object does the `pipe` method accept?

I have the following code:
process.stdin.pipe(output).pipe(socket);
I want to modify the contents of output before it is piped into socket. I need to add an additional pipe in between the two, but I am not sure how to do that. I checked the documentation on Streams but I could not figure out how to create my own method to invoke pipe with that will intercept the current pipeline.
What kind of object does the pipe method accept?

How do I get a basic setup of piping stdout to a file in Nodejs?

This seems basic enough, but I can't seem to get it to work.
var access = require( 'fs' ).createWriteStream( 'logs/test.access.log', { flags : 'a' } );
process.stdout.pipe( access );
I'm assuming that when I use console.log() after doing this, wouldn't that message be written to test.access.log? I don't get any errors, but I simply don't get anything in the file as well, so I'm wondering if someone can help me understand streams and writing from stdout to a log file like I have above.
Thanks!
Most likely, your Node.js app is closing before the stream's buffer is flushed to disk. This is probably because you're piping the process's own stdout into a file, and stdout normally doesn't close and flush until after Node.js starts to clean up the process.
Try sticking access.destroySoon(); into the end of your code and see if that causes it to flush the data to disk before ending the Javascript event loop.

Resources