Found 6683 Articles for Javascript

Stream writable.writableLength Property in Node.js

Mayank Agarwal
Updated on 20-May-2021 13:33:30

156 Views

The writable.writableLength property is used for displaying the number of bytes or objects which are there in the queue that are ready to be written. This is used for inspecting the data as per the status from highWaterMark.Syntaxwriteable.writableLengthExample 1Create a file with name – writableLength.js and copy the below code snippet. After creating file, use the following command to run this code as shown in the example below −node writableLength.js Live Demo// Program to demonstrate writable.writableLength method const stream = require('stream'); // Creating a data stream with writable const writable = new stream.Writable({    // Writing the data from stream ... Read More

Stream writable.cork() and uncork() Method in Node.js

Mayank Agarwal
Updated on 20-May-2021 13:30:16

477 Views

The writable.cork() method is used for forcing all the written data to be buffered inside a memory. This buffered data will only be removed from the buffer memory after stream.uncork() or stream.end() method have been called.Syntaxcork()writeable.cork()uncork()writeable.uncork()ParametersSince it buffers the written data. Only parameter that's needed will be the writable data.ExampleCreate a file with name – cork.js and copy the below code snippet. After creating file, use the following command to run this code as shown in the example below −node cork.jscork.js Live Demo// Program to demonstrate writable.cork() method const stream = require('stream'); // Creating a data stream with writable const ... Read More

urlObject.auth() Method in Node.js

Mayank Agarwal
Updated on 20-May-2021 13:29:12

76 Views

The auth() property defines the username and password portion of any URL, also called as userInfo. The string and username are separated by a colon ( : ).SyntaxurlOject.auth()ParametersSince it retuns only the username and password from a URL, it does not required any input parameters.ExampleCreate a file with name – auth.js and copy the below code snippet. After creating file, use the following command to run this code as shown in the example below −node auth.jsauth.js Live Demo// Importing the URL module const url = require('url'); var adr = 'https://username=hello:password=tutorialspoint@www.tutorialspoint.com/'; // Parsing the above URL address var q = ... Read More

send(), sendStatus() and json() method in Node.js

Mayank Agarwal
Updated on 20-May-2021 13:28:45

4K+ Views

The send() and json() functions are used for sending the response to the client directly from the server. The send() method will send the data in a string format, whereas the json() function will send the same in JSON format. The sendStatus() method is used for sending the HTTP request status with the client. Possible status values are: 200(Success), 404(Not found), 201(Created), 503(Server Unreachable) etc.PrerequisiteNode.jsExpress.jsInstallationInstall the express module using the below statement −npm install expressExample - sendStatus()Create a file with name – sendStatus.js and copy the below code snippet. After creating file, use the following command to run this code ... Read More

script.createCachedData() Method in Node.js

Mayank Agarwal
Updated on 20-May-2021 13:24:12

120 Views

The script.createCachedData() method is used for creating a code cache that will be used along with the cachedData option of the script constructor. This cachedData can be called multiple number of times without latency. This method is an inbuilt programming interface from the 'script' module.Syntaxscript.createCachedData()ParametersSince it only caches the data. It does not require any specific inputs from the user. It only returns the cached buffer.ExampleCreate a file with name – createCachedData.js and copy the below code snippet. After creating file, use the following command to run this code as shown in the example below −node createCachedData.jscreateCachedData.js// Node.js program to ... Read More

Reading a text file into an Array in Node.js

Mayank Agarwal
Updated on 20-May-2021 13:23:16

5K+ Views

We can read a text file and return its content as an Array using node.js. We can use this array content to either process its lines or just for the sake of reading. We can use the 'fs' module to deal with the reading of file. The fs.readFile() and fs.readFileSync() methods are used for the reading files. We can also read large text files using this method.Example (Using readFileSync())Create a file with name – fileToArray.js and copy the below code snippet. After creating file, use the following command to run this code as shown in the example below −node fileToArray.jsfileToArray.js// ... Read More

process.env() Method in Node.js

Mayank Agarwal
Updated on 20-May-2021 13:22:48

346 Views

The process.argv() method is used for getting the user environment object. This object will contain all the information about the environment on which this command is being executed.Syntaxprocess.env()ParametersSince it returns an object for the user environment. It does not need any inputs from the user as such.ExampleCreate a file with name – env.js and copy the below code snippet. After creating file, use the following command to run this code as shown in the example below −node env.jsenv.js Live Demo// Node.js program to demonstrate the use of process.env // Importing the process module const process = require('process'); // Printing ... Read More

process.cpuUsage() Method in Node.js

Mayank Agarwal
Updated on 20-May-2021 13:18:00

1K+ Views

The process.argv() method is used for getting the user and its cpu usage for the current running process. The data is returned in an object with the properties user and system. The values obtained are in microseconds, i.e.10^-6 seconds. The values returned may be greater than the actual elapsed time if multiple cores are performing work for the running process.Syntaxprocess.cpuUsage([previousValue])ParametersThe method only accepts a single parameter which is defined below −previousValue – This is an optional parameter. This is the previous return value by calling the process.cpuUsage() method.ExampleCreate a file with name – cpuUsage.js and copy the below code snippet. After ... Read More

process.chdir() Method in Node.js

Mayank Agarwal
Updated on 20-May-2021 13:17:30

5K+ Views

The process.chdir() method is used for changing the current directory of the Node.js process. It will throw an exception if any error occurs or the process fails, but will not return any response on success. For Example: It can fail when the specified directory does not exist.Syntaxprocess.chdir(directory)Parametersdirectory – This will contain the name of the directory that will be updated in place of earlier directory name.ExampleCreate a file with name – chdir.js and copy the below code snippet. After creating file, use the following command to run this code as shown in the example below &Minus;node chdir.jschdir.js Live Demo// Node.js program to ... Read More

process.argv0() Method in Node.js

Mayank Agarwal
Updated on 20-May-2021 13:14:00

170 Views

The process.argv0() method is used for storing the read-only copy of the original value for argv[0] that is passed when the node.js application is started.Syntaxprocess.argv0()ParametersSince it returns only the read-only copy for stored value of argv[0]. It does not need any inputs from the user.ExampleCreate a file with name – argv0.js and copy the below code snippet. After creating file, use the following command to run this code as shown in the example below −node argv0.jsargv0.js Live Demo// Node.js program to demonstrate the use of process.argv0 // Importing the process module const process = require('process'); // Printing argv0 value ... Read More

Advertisements