Found 254 Articles for Node.js

crypto.createDiffieHellman() Method in Node.js

Mayank Agarwal
Updated on 20-May-2021 11:50:54

243 Views

The above method creates a DiffieHellman key exchange object with the help of the supplied prime value and an optional specific generator. The generator argument can hold either a string, number or Buffer value. Default value for generator is 2.Syntaxcrypto.createDiffieHelmmanGroup(prime, [primeEncoding], [generator], [generatorEncoding]ParametersThe above parameters are described as below −prime – The number of prime bits that will be generated. Input value is of type number.primeEncoding – This parameter defines the encoding of the prime string. Possible input types are: string, buffer, TypedArray and DataView.generator – Generator for generating the exchange key object. Default value: 2.generatorEncoding – This parameter defines the generator string encoding.ExampleCreate ... Read More

assert.deepStrictEqual() function in Node.js

Mayank Agarwal
Updated on 20-May-2021 11:24:54

189 Views

The assert module provides a bunch of different functionalities that are used for function assertion. One of them is deepStrictEqual() function. This function is used to test the deep equality between the actual and expected parameters. An assertion error will be raised if the condition is not fulfilled.Syntaxassert.deepStrictEqual(actual, expected[, message])ParametersThe above parameters are described as below −actual – This is the actual value that will be evaluated against the expected parameters.expected – This is the expected parameter value which is matched against the actual value.message – This parameter holds the string message value to be printed if the actual and expected parameters do ... Read More

Logging in Node.js

Mayank Agarwal
Updated on 20-May-2021 11:24:21

315 Views

Logging is a very essential part in any application whether it is made in Node.js or any other programming languages. Logging helps us to detect weird behaviours of an application along with real-time errors and exceptions. One should definitely put logical logs in their application. These logs help the user to identify any mistakes and resolve it on urgent basis.There are 5 different log levels which are present at the moment with the user. These log levels are used to define different kinds of logs and helps the user to identify different scenarios. The log levels must be carefully configured ... Read More

Integrating Express-rate-limit in Node.js

Mayank Agarwal
Updated on 20-May-2021 11:23:20

305 Views

Rate-limiting is becoming important day by day to prevent websites from DOS & DDOS attacks. The rate-limiting prevents the system from any type of fake requests or other brute force attacks. Rate limiting limits the number of times an IP can make requests. The expressrate-limit is the npm package to limit the number of requests from a user.Installing the rate-limit moduleRun the below command to install the express rate-limiting module in your application.npm install --save express-rate-limitExampleCreate a file with name – rateLimit.js and copy the below code snippet. After creating file, use the following command to run this code as ... Read More

Creating custom modules in Node.js

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

2K+ Views

The node.js modules are a kind of package that contains certain functions or methods to be used by those who imports them. Some modules are present on the web to be used by developers such as fs, fs-extra, crypto, stream, etc. You can also make a package of your own and use it in your code.Syntaxexports.function_name = function(arg1, arg2, ....argN) {    // Put your function body here... };Example - Custom Node ModuleCreate two file with name – calc.js and index.js and copy the below code snippet.The calc.js is the custom node module which will hold the node functions.The index.js ... Read More

Creating an Agent in Node.js

Mayank Agarwal
Updated on 20-May-2021 11:22:19

843 Views

You can use new Agent() method to create an instance of an agent in Node. The http.request() method uses the globalAgent from the 'http' module to create a custom http.Agent instance.Syntaxnew Agent({options})ParametersThe above function can accept the following Parameters −options – These options will contain the configurable options that could be set on an Agent while creation. Below are the fields/options the Agent can have −keepAlive – This method keeps the sockets around whether there are any outstanding requests or not, but keeps them for any future requests without actually re-establishing the TCP connection. One can use 'close' connection' to close this connection. ... Read More

Changing the npm start-script of Node.js

Mayank Agarwal
Updated on 20-May-2021 11:21:38

10K+ Views

The start-script of a Node.js application consists of all the commands that will be used to perform the specific tasks. When starting or initializing a Node.js project, there are a lot of predefined scripts that will be created for running the application. These scripts can be changed as per the need or demand of the project.Script commands are widely used for making different startup scripts of programs in both Node and React. 'npm start' is used for executing a startup script without typing its execution command.Package.json FileThis is the start-up script that needs to be added in the package.json file. ... Read More

agent.createConnection() Method in Node.js

Mayank Agarwal
Updated on 20-May-2021 11:20:24

545 Views

The agent.createConnection() method is an interface provided by the 'http' module. This method produces a socket/stream that can be used for the HTTP requests. One can use custom agents to override this method for greater flexibility. A socket/stream can be returned in two ways – either by returning the socket/stream directly from this function, or by passing this socket/stream to the callback.Syntaxagent.createConnection(options, [callback])ParametersThe above function can accept the following parameters −options – These options will contain the connection details for which stream has to be created.callback – This will receive the created socket connection from the agent.ExampleCreate a file with the name ... Read More

writeJson() function in fs-extra - NodeJS

Mayank Agarwal
Updated on 28-Apr-2021 07:36:01

1K+ Views

writeJson() writes an object to a JSON file by parsing.SyntaxwriteJson(file, object[, options] [, callback])Parametersfile – String parameter which will contain name and location of the JSON file.object – Object passed into the writeJson function.options – The 'outputFile' function supports the following options −spaces – The number of spaces will be passed in this parameter for indentation.EOL – Setting the 'end of line' character, Default is ''.replacer – It takes two parameters – key and value. Will replace If key found, the value will be replaced by the given value.callback – This function will give a callback if any error occurs.Example 1Check that fs-extra is installed before proceeding; ... Read More

URLSearchParams values & keys() in Node

Mayank Agarwal
Updated on 28-Apr-2021 07:31:36

367 Views

Introduction to values()This function returns an iterator that allows us to iterate all over the values that are present in that object. It basically gives us a tool to select or iterate the values and then perform functions on them.SyntaxURLSearchParams.values();It will return an ES6 type iterator with the name-value pair over all the values.Example// Defining the parameters as a constant var params = new URLSearchParams( 'key1=value1&key2=value2&key3=value3'); // Iterating over the values of params for(var value of params.values()) {    console.log(value); }Outputvalue1 value2 value3Example// Defining the URL as a constant const params = new URLSearchParams('name=John&age=21'); // Iterating over the ... Read More

Advertisements