How to parse JSON files in Node.JS ?

Quick tip using fs: let data = fs.readFileSync(“myFile.json”, ‘utf8’); console.log(typeof JSON.parse(data)); There is another way to do this that requires less effort. Using require directly, like so: const data = require(“myFile.json”); console.log(typeof data); Hope it helps.

How to pack NodeJS project into a tarball?

Today I needed to install my brand new NodeJS-developed CLI application in my computer. After a quick search on the Internet I became aware that NPM allows me to do that very easily. The command is: npm pack Run it inside the project that you want to pack (at the same level of the package.json file). Here is an example …

How to create a Node.JS application with Express Generator

I am assuming that you already have Node.JS installed. If you don’t, worry not! you can get your own copy today from the Node.JS download section! Using ExpressJS is very quick and easy. It generates all the file structure needed for a web application. You can select¬† any of the different views they have available, in this case I am …

How to Encrypt / Decrypt with AES (CCM & GCM) in Node.JS

First of all we have to understand what is CCM mode and GCM mode. Roughly: CCM CCM mode (Counter with CBC-MAC) is a mode of operation for cryptographic block ciphers. It is an authenticated encryption algorithm designed to provide both authentication and confidentiality. CCM mode is only defined for block ciphers with a block length of 128 bits. GCM Galois/Counter …

How to Encode / Decode to / from base64 in Node.JS

Today I ran into this basic problem. A quick search on google gave me the answer, I tested it, it worked, and the rest is history. Here is the code. let plainText = “Hello World!”; console.log(“Plaint text = ” + plainText); // Encoding to base64 let base64 = new Buffer(plainText).toString(‘base64’); console.log(“Encoded to base64 = ” + base64); // Decoding from …

How to convert a String into JSON object in NodeJS

Just a few days ago I needed to do this and I found this useful function. Very simple: var jsonStr = “{“firstName”:”Siry”, “lastName”:”Mazari”}”; console.log(‘JSON String: %s’, jsonStr); var jsonObj = JSON.parse(jsonStr); console.log(‘First Name: %s’, jsonObj.firstName); console.log(‘Last Name: %s’, jsonObj.lastName); The variable jsonStr contains a String representation of a JSON object. The variable jsonObj contains the object that was parsed from …