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) […]

PHP Notice: Undefined offset: 1 (Array)

How to fix PHP Notice: Undefined offset: 1? This PHP snippet will generate a “Undefined Offset” notice: $parts = explode(".", "aaaa");   $data = $parts[1];   print_r($data);$parts = explode(".", "aaaa"); $data = $parts[1]; print_r($data); This happens because $parts[1] is undefined. To fix this “Undefined Offset” notice just check if isset($parts[1]): $parts = explode(".", "aaaa");   […]

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++) { […]

How to Install a Module/Package/Library in NodeJs

Do you need to install a NodeJs module/package/library? First create the project directory: mkdir TestProject cd TestProjectmkdir TestProject cd TestProject Make the directory the root of your project: npm init –yesnpm init –yes Install the required NodeJs modules/packages (i.e “request”): npm install request –savenpm install request –save * With -save parameter, the installed modules will […]

NodeJs Request.Get Example with Error Handling

Here is an example of using NodeJs “request” library with error handling: var dateformat = require(‘dateformat’); var request = require(‘request’);   request.get(‘https://www.google.com/’,function(err, res, body) { if(err) console.error(‘[Request.Get] %s Error:’, dateformat(new Date(), "yyyy-mm-dd h:MM:ss"), err.message); console.log(‘[Request.Get] %s StatusCode:’, dateformat(new Date(), "yyyy-mm-dd h:MM:ss"), res.statusCode); });var dateformat = require(‘dateformat’); var request = require(‘request’); request.get(‘https://www.google.com/’,function(err, res, body) { if(err) […]