Latest in Data Processing

HomeData Processing

Detect user country by IP in Node.js

It shouldn't be hard to detect a user's country based on the IP address, right? Right... I really liked the simplicity of ip2location module: load the BIN file, get the location, and close the BIN file.
Prerequisites Before you install the module, you will need to download the BIN file by registering at ip2location and choosing the appropriate option. You will have a choice between the country, country + city, or other options....

Convert all JPGs and PNGs in a folder to WEBP and AVIF with Node.js and Sharp

One of the easiest ways to convert all JPG and PNG images in a folder to WEBP and AVIF is to use the Sharp library.
Let's get straight to the point. We will need to install sharp and glob.
npm install --save glob sharp
With that done, we now need to write a relatively simple loop to find all images and convert them to WEBP and AVIF. We will...

Convert all PNGs in a folder to WEBP with FFmpeg

FFmpeg is an awesome tool that can convert not only videos but also audio and images.
On Linux and Mac, run it like this in the folder where you want the conversion to run:
for i in *.png; do ffmpeg -y -i "$i" -c:v libwebp "${i%.*}.webp"; done
On Windows:
FOR /F "tokens=*" %G IN ('dir /b *.png') DO ffmpeg...

Convert bytes to megabytes, gigabytes, terabytes without a loop.

What if I tell you you can convert bytes to petabytes without doing a loop and constantly dividing by 1024?
function bytesToHuman(bytes) { let prefixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; let base = 1024; let cl = Math.min(parseInt(Math.log(bytes) / Math.log(base), 10), prefixes.length - 1); return (bytes / Math.pow(base, cl)).toFixed(2) + ' ' + prefixes[cl]; } console.log(bytesToHuman(1024 * 1024 * 10)); // 10.00 MB console.log(bytesToHuman(2151099283)); // 2.00 GB
You can convert HTML and CSS to SVG directly in a browser. With JavaScript. And JSX.
The developers of Next.js created this neat library called Satori, which allows to convert JSX elements with style properties into SVG. As a result, this gives amazing possibilities:
You can generate HTML previews. For example, you can generate a simple JSX element on the server, convert to SVG, and then later convert it to JPG or...

About Code with Node.js

This is a personal blog and reference point of a Node.js developer.

I write and explain how different Node and JavaScript aspects work, as well as research popular and cool packages, and of course fail time to time.