Found 10483 Articles for Web Development

Integrating Express-rate-limit in Node.js

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

407 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

3K+ 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

1K+ 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

773 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

Implementing insertion sort to sort array of numbers in increasing order using JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 09:54:28

2K+ Views

The art of sorting arrays is of paramount importance in the realm of programming, as it allows for efficient organization and manipulation of data. When it comes to implementing a reliable sorting algorithm, insertion sort emerges as a versatile and effective option. In this article, we delve into the intricate world of JavaScript to explore the process of implementing insertion sort to arrange an array of numbers in increasing order. By comprehending the underlying mechanics of this algorithm and harnessing the power of JavaScript's capabilities, developers can unlock the potential to efficiently sort and organize numerical data, enhancing the performance ... Read More

Regrouping characters of a string in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:35:22

156 Views

ProblemWe are required to write a JavaScript function that takes in a string str as the first and the only argument.The string str can contain three types of characters −English alphabets: (A-Z), (a-z)Numerals: 0-9Special Characters − All other remaining charactersOur function should iterate through this string and construct an array that consists of exactly three elements, the first contains all alphabets present in the string, second contains the numerals and third the special characters maintain the relative order of characters. We should finally return this array.For example, if the input to the function isInputconst str = 'thi!1s is S@me23';Outputconst output ... Read More

Maximum average of a specific length of subarray in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:34:13

149 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers, arr, as the first argument and a number, num, as the second argument.Our function should find the contiguous subarray of given length num that has the maximum average value. And we need to output the maximum average value.For example, if the input to the function isInputconst arr = [1, 12, -5, -6, 50, 3]; const num = 4;Outputconst output = 12.75;Output ExplanationBecause the desired subarray is [12, -5, -6, 50]ExampleFollowing is the code − Live Democonst arr = [1, 12, -5, -6, 50, 3]; const num ... Read More

Finding longest consecutive joins in JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:33:48

145 Views

ProblemWe are required to write a JavaScript function that takes in an array of pairs of numbers, arr, as the first and the only argument. In every pair, the first number is always smaller than the second number.Now, we define a pair (c, d) that can follow another pair (a, b) if and only if b < c. Chain of pairs can be formed in this fashion. Our function is supposed to find the length longest chain which can be formed.For example, if the input to the function isInputconst arr = [    [1, 2], [2, 3], [3, 4] ];Outputconst ... Read More

Finding two closest elements to a specific number in an array using JavaScript

AmitDiwan
Updated on 24-Apr-2021 10:33:16

802 Views

ProblemWe are required to write a JavaScript function that takes in an array of sorted integers, arr, as the first argument and a target number, as the second argument.Our function should return an array of exactly two numbers that exists in the array arr and are closest to target. The output array should also be sorted in increasing order.For example, if the input to the function isInputconst arr = [1, 2, 3, 4, 5]; const target = 3;Outputconst output = [2, 3];ExampleFollowing is the code − Live Democonst arr = [1, 2, 3, 4, 5]; const target = 3; const findClosest ... Read More

Advertisements