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

NodeJs HTTPS.Get Example with Error Handling

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

How to Find PHP.ini Location

Need to know where is located your PHP.ini file? Locate PHP.ini with Find The best way (in my opinion) is to use “find” to locate the php.ini: find / -name php.inifind / -name php.ini You should get something like this: /etc/php/7.3/fpm/php.ini /etc/php/7.3/cli/php.ini/etc/php/7.3/fpm/php.ini /etc/php/7.3/cli/php.ini So this way you will locate PHP.ini for both PHP7 FPM and […]

How to Enable Display of Errors in PHP

First edit your PHP.ini file to make sure display_errors is enabled: display_errors = ondisplay_errors = on Then if you changed this value, you’ll need to restart PHP7-FPM process: /etc/init.d/php7.3-fpm restart/etc/init.d/php7.3-fpm restart Additionally you may prepend these lines on your PHP script: error_reporting(E_ALL); ini_set(‘display_errors’, 1); ini_set(‘display_startup_errors’, 1);error_reporting(E_ALL); ini_set(‘display_errors’, 1); ini_set(‘display_startup_errors’, 1);

Go One Directory Level Up/Back with Dirname() in PHP

For PHP < 5.3 use this: echo realpath(dirname(__FILE__) . '/..');echo realpath(dirname(__FILE__) . '/..'); For PHP 5.3 to 5.6 use this: echo realpath(__DIR__ . '/..');echo realpath(__DIR__ . '/..'); For PHP >= 7.0 use this: echo dirname(__DIR__, 1);echo dirname(__DIR__, 1); References: https://www.php.net/manual/en/function.dirname.php

Save a String to File in Python

A simple way to save a string (or whatever) to a file in Python is: file = open(‘File.txt’, ‘w’) file.write(‘whatever’) file.close()file = open(‘File.txt’, ‘w’) file.write(‘whatever’) file.close() An alternative method that auto-closed the file is: with open(‘File.txt’, ‘w’) as file: file.write(‘whatever’)with open(‘File.txt’, ‘w’) as file: file.write(‘whatever’)

Create a File Name with Current Date & Time in Python

In Python you can get the current date and time like this: import time timestr = time.strftime("%Y-%m-%d_%H-%M-%S") print(timestr)import time timestr = time.strftime("%Y-%m-%d_%H-%M-%S") print(timestr) Here is an example output: 2019-08-11_15-23-022019-08-11_15-23-02 Then you can use timestr variable as file name, i.e: file = open(timestr+’.txt’, ‘w’) file.write(‘whatever’) file.close()file = open(timestr+’.txt’, ‘w’) file.write(‘whatever’) file.close() Or another example below: with […]