How to Download a File in NodeJS without any Third Party Libraries

If you need to download a file in NodeJS without using any third party libraries, then you can do the following. The NodeJS ecosystem comes with a fs module, this is to denote the FileSystem built in library. First declare your imports: const http = require('https'); const fs = require('fs'); Now you can use the https module to download a file, and write it to a stream using fs. const https = require("https"); const fs = require("fs"); const endpoint = "https://example/files/some_csv_file"; https....

October 26, 2022 · 1 min · 94 words · Andrew

How to Execute a Shell Script in NodeJS

If you need to execute a shell script in NodeJS, then you can use the exec keyword. Syntax: exec(command [, options] [, callback] const shell = require('shelljs') shell.exec("npm --version")

October 25, 2022 · 1 min · 29 words · Andrew

How to Convert Milliseconds to Date in Javascript

If you need to convert Milliseconds to Date in Javascript, then you can do the following: How to Convert Milliseconds to Date in Javascript let date = new Date(milliseconds); date.toString(); Common Date Conversions From Milliseconds in Javascript let originalDate = new Date(milliseconds); // output: "D/MM/YYYY, H:MM:SS PM/AM" originalDate.toLocaleString(); //output: "D/MM/YYYY" originalDate.toLocaleDateString(); // output: "Day Month DD YYYY" originalDate.toDateString(); // output: "HH:MM:SS GMT+0530" originalDate.toTimeString(); // output: "Day Month DD YYYY HH:MM:SS GMT+0500" originalDate....

October 14, 2022 · 1 min · 77 words · Andrew

How to Convert String to Title Case in Javascript

If you need to convert a String to Title Case in Javascript, then you can do one of the following: Option 1 – Using a for loop function titleCase(str) { str = str.toLowerCase().split(' '); for (var i = 0; i < str.length; i++) str[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1); return str.join(' '); } console.log(titleCase("this is an example of some text!")); Output: This Is An Example Of Some Text! Option 2 – Using map() function titleCase(str) { return str....

October 13, 2022 · 1 min · 134 words · Andrew

How to Deploy React App to S3 and CloudFront

If you would like to deploy a React App to AWS S3 and AWS CloudFront, then you can follow this guide. The following solution creates a React App and deploys it to S3 and CloudFront using the client’s CLI. It also chains commands so that a React build, S3 sync and CloudFront invalidation can occur with a single command. Code available at GitHub Target Architecture Guided Deployment Solution Create a directory for the application:...

August 22, 2022 · 9 min · 1884 words · Andrew

[Solved] export ‘Switch’ (imported as ‘Switch’) was not found in ‘react-router-dom’

In react-router-dom v6, Switch is replaced by routes Routes. You need to update the import from: import { Switch, Route } from "react-router-dom"; to: import { Routes, Route } from 'react-router-dom'; You also need to update the Route declaration from: <Route path="/" component={Home} /> to: <Route path='/welcome' element={<Home/>} />

August 21, 2022 · 1 min · 49 words · Andrew

How to get all checked checkboxes in Javascript

If you need to get all the checked checkboxes using Javascript, then you can do the following: Option 1 – In a single line const checkedBoxes = document.querySelectorAll('input[name=mycheckboxes]:checked'); Option 2 – Another one liner const data = [...document.querySelectorAll('.mycheckboxes:checked')].map(e => e.value); Option 3 – Create a helper function function getCheckedBoxes(checkboxName) { var checkboxes = document.getElementsByName(checkboxName); var checkboxesChecked = []; for (var i=0; i<checkboxes.length; i++) { if (checkboxes[i].checked) checkboxesChecked.push(checkboxes[i]); } } return checkboxesChecked....

July 26, 2022 · 1 min · 88 words · Andrew

How to Create a Hashtag Generator in Javascript

If you want to create a hashtag generator in Javascript, then you can do the following: function generateHashtag(string) { if (string.trim() === '') return false; const stringWithCamelCase = string .split(' ') .map(word => word.charAt(0).toUpperCase() + word.slice(1)) .join(''); const stringWithHashtag = `#${stringWithCamelCase.trim()}`; return stringWithHashtag.length > 140 ? false : stringWithHashtag; } How to use the Hashtag Generator const hashtag = generateHashtag("My New Hashtag !") console.log(hashtag); // #MyNewHashtag!

July 23, 2022 · 1 min · 66 words · Andrew

How to Confirm before Leaving Page in Javascript

You can implement a function to be called before the user leaves a page with Javascript as follows: window.onbeforeunload = function(e) { return "Do you want to exit this page?"; }; This is a typically solution for when you want the user to confirm their changes are saved before allowing them to close the current page in editing tools.

July 22, 2022 · 1 min · 59 words · Andrew

How to Style an Element using Javascript

If you need to style an element using Javascript then you can use the style object to support all your CSS needs. <html> <body> <p id="p1">Hello World!</p> <script> document.getElementById("p1").style.color = "red"; </script> <p>The paragraph above was changed by a script.</p> </body> </html> If you need to do this purely in a Javascript file itself, then: // Get a reference to the element var myElement = document.querySelector("#p1"); // Now you can update the CSS attributes myElement....

July 15, 2022 · 1 min · 107 words · Andrew