Category Archives: NodeJs

NodeJS ETIMEDOUT, ESOCKETTIMEDOUT, ENOTFOUND

While using request package with a NodeJS script I noticed it was causing a lot of errors like ETIMEDOUT, ESOCKETTIMEDOUT and also getaddrinfo ENOTFOUND after some HTTP requests on the same URL. That was very strange because the remote URL was working correctly… So I started to analyze a few things and I noticed that […]

NodeJS Disable SSL Verification on Request

You can set strictSSL to false on NodeJs request: var options = { url: ‘http://url’, strictSSL: false }   request(options, function(err, resp, body) {});var options = { url: ‘http://url’, strictSSL: false } request(options, function(err, resp, body) {}); More information can be found here: https://github.com/request/request#user-content-requestoptions-callback strictSSL – if true, requires SSL certificates be valid. Note: to […]

Modify NodeJS Request Default Timeout

You can modify the default request timeout like this: var options = { url: ‘https://www.google.com/’, timeout: 120000 }   request(options, function(err, resp, body) {});var options = { url: ‘https://www.google.com/’, timeout: 120000 } request(options, function(err, resp, body) {}); Details of the param timeout can be found here: https://github.com/request/request#user-content-requestoptions-callback timeout – integer containing number of milliseconds, controls […]

How to Permanently Run NodeJs Script in Background

You can use the Node package named Forever to run continuously a NodeJs script. The advantage of using “forever” package is that it can make a NodeJs script execute forever even after the node script process is killed, because it gets respawned! 1) Install Node “forever” package npm install forever -gnpm install forever -g 2) […]

How to Stop NodeJs Puppeteer Follow Redirects

Coupling setRequestInterception() with request.isNavigationRequest() and request.redirectChain() you can now abort navigation redirects in Puppeteer like this: await page.setRequestInterception(true); page.on(‘request’, request => { if (request.isNavigationRequest() && request.redirectChain().length) request.abort(); else request.continue(); }); await page.goto(‘https://example.com’);await page.setRequestInterception(true); page.on(‘request’, request => { if (request.isNavigationRequest() && request.redirectChain().length) request.abort(); else request.continue(); }); await page.goto(‘https://example.com’); References: https://github.com/GoogleChrome/puppeteer/issues/1132#issuecomment-393724933

NodeJs How to Get GET Query String in Express.js

With Express.js you can use req.query like this: var express = require(‘express’); var app = express();   app.get(‘/’, function(req, res){ res.send(‘id: ‘ + req.query.id); });   app.listen(8088);var express = require(‘express’); var app = express(); app.get(‘/’, function(req, res){ res.send(‘id: ‘ + req.query.id); }); app.listen(8088); It handles the $_GET[‘id’] GET query, i.e: http://127.0.0.1:8088/?id=123http://127.0.0.1:8088/?id=123

NodeJs How to Split/Splice Array in Chunks

To split an array in chunks in NodeJs you can use splice(): var a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]; var b = a.splice(0,10);   //a is now [11,12,13,14,15]; //b is now [1,2,3,4,5,6,7,8,9,10];var a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]; var b = a.splice(0,10); //a is now [11,12,13,14,15]; //b is now [1,2,3,4,5,6,7,8,9,10]; You can also loop the array and do something with the […]

NodeJS Error: Socket Hang Up (ECONNRESET)

Users have reported this ECONNRESET error with request.get(): Error: { Error: socket hang up at TLSSocket.onHangUp (_tls_wrap.js:1137:19) at Object.onceWrapper (events.js:313:30) at emitNone (events.js:111:20) at TLSSocket.emit (events.js:208:7) at endReadableNT (_stream_readable.js:1064:12) at _combinedTickCallback (internal/process/next_tick.js:138:11) at process._tickCallback (internal/process/next_tick.js:180:9) code: ‘ECONNRESET’, path: null, host: ‘google.com’, port: 443 }Error: { Error: socket hang up at TLSSocket.onHangUp (_tls_wrap.js:1137:19) at Object.onceWrapper (events.js:313:30) […]

How to Sleep/Wait/Pause on a NodeJs Function

I found this example code that shows how to sleep in NodeJs: function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); }   async function demo() { console.log(‘Taking a break…’); await sleep(2000); console.log(‘Two seconds later, showing sleep in a loop…’);   // Sleep in loop for (let i = 0; i < 5; i++) { […]