GetCoding
  • Home
  • Courses
  • Contact
Category:

Uncategorized

Uncategorized

How to read environment variables with Node.js

by Matt Heff 2 months ago
written by Matt Heff

Node.js is a fast, scalable, and efficient framework in the web development community, known for its event-driven, nonblocking input/output structure. Node.js also has a convenient way of working with environment variables, allowing for easy configuration of a Node.js application

How to check you have Node.js installed on your machine

node -v

If it is installed you will see the version number returned, if node is not present you will get a error form the command line which is equivalent of saying

Gary Coleman Whatchu Talkin Bout Willis GIF - Gary Coleman Whatchu Talkin  Bout Willis Confused - Discover & Share GIFs

How to install Node.js

  1. Go to the Node.js Downloads page.
  2. Download Node.js for your Operating System
  3. Run the installer, including accepting the license, selecting the destination, and authenticating for the install.

Now when you open a new console and run node -v the command line will return the version you just installed

Using Node.js to access environment variables two Methods

1 – Access the environment variables already present

In Node.js, you can easily access variables set in your computer’s environment. When your Node.js program starts, it automatically makes all environment variables available through an “env” object. You can see the object by running the command “node” in your command line and then typing “process.env”.

You should see something like this

If we want to access a specific parameter, lets use SHELL for example we would want to run a command such as

console.log(`Shell is parameter is ${process.env.SHELL}`);

With this you can check what variables exist within the environment, you can update them, add some but these will only be available to that individual node process and any children it spawns. This is easy to access but it is not the best when you are working on multiple projects and need to specify environmental variable settings per project. For that we have another approach

2 – Access Environmental Variable using DOTENV

Dotenv is a module in Node.js allows you to easily load environment variables from a .env file in your project. This file should contain key-value pairs, with the keys being the names of the environment variables and the values being the values you want to set for those variables. Once you have your .env file set up, you can use the dotenv module to load the variables into your Node.js application. You will want to keep sensitive information in here examples of such items are API keys, besides that you can use it for anything else you don’t want to hard-code into your application.

Important: If you use git or other version controls make sure you do not include .env in version tracking. The last thing you need is this sensitive data pushed into a repo for someone to exploit.

Installing dotenv
npm install dotenv --save

After it has been installed, add the following line to the top of your js file

require('dotenv').config();

Create a .env File

After this is installed, you need to make a file called .env and put it in the top level of your projects file / folder structure. This file is the place to put all your environmental variables. When entering your variables there is no need to wrap strings in quotation marks, DotEnv does this automatically for you, below is an example .env file.

Example .env file contents

DB_HOST=db.getcoding.io
DB_USER=admin
DB_PASSWORD=password
DB_NAME=example

A example of a DB connection using the DB_HOST parameter

const dotenv = require('dotenv');
const { Client } = require('pg');

// Load environment variables from .env file
dotenv.config();

// Get the value of the DB_HOST environment variable
const serverName = process.env.DB_HOST;

// Create a new PostgreSQL client
const client = new Client({
  host: serverName,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
});

// Connect to the database
client.connect()
  .then(() => console.log(`Connected to PostgreSQL on ${serverName}`))
  .catch(err => console.error(`Error connecting to PostgreSQL at ${serverName}: ${err}`));

Once you have done this and saved it to a file (eg env-example.js) then run the file and see what happens

node env-example.js

Depending if you have an actual postgres Db that you can access with username/pass/database with the values above you will either see

Connected to PostgreSQL on db.getcoding.io
or

Error connecting to PostgreSQL at db.getcoding.io: Error: getaddrinfo ENOTFOUND db.getcoding.io

Other Issues — Package PG not found

If your environment does not have the required postgres package then you can installed it by using the following

npm install pg --save

Summary

Woo Hoo!! We have just learned and hopefully experimented two methods for using environmental variables in Node.js: dotenv and directly from the environment. Armed with this knowledge you can now easily store and access any information your projects in a secure and convenient way.

Have fun with it.

2 months ago 0 comment
TwitterLinkedin
Uncategorized

AI – What is Natural Language Processing (NLP)

by Matt Heff 6 months ago
written by Matt Heff

G’day mates! Are you curious about how computers can understand and generate human language? Well, you’re in luck – I also wanted to find out about AI and language. Since GPT has come along my obsession has turned towards Generative AI and AI in general, it has been a while since i studied AI and boy oh boy has the field come a long way since then so let me share what i know about natural language processing (NLP).

NLP is a field of artificial intelligence (AI) that focuses on making computers smart enough to understand and generate human language. It’s used in all sorts of applications, from chatbots to language translation to text analysis. And it’s an exciting and rapidly-evolving field that’s full of opportunities and challenges. At the moment this type of AI has exploded and looks to have advanced to a level that most of us that work with information will be able to benefit from its assistance. But how does it work?

At a high level, NLP involves two main stages: understanding language and generating language. Understanding language involves analyzing a piece of text and figuring out what it means, while generating language involves using a computer to produce text that sounds like it was written by a human. At this moment i personally believe with the correct prompt engineering you can get useable text/language from the AI.

To do this, NLP systems use a combination of rules and machine learning algorithms. Rules-based systems use pre-defined rules and patterns to analyze and generate language, while machine learning systems learn from examples of human language to develop their own rules and patterns. It’s like teaching a wee tot how to speak English – it takes a bit of training, but once they get the hang of it, they can be pretty darn good at it!

Overall, NLP is a fascinating field of AI that looks like it is going to revolutionize how we interact with technology and how we communicate. If you’re interested in computers and language, you should definitely check out NLP – it might just be the perfect field for you! And who knows, with a bit of hard work and a positive attitude, you might just be the one to teach that wee tot how to speak English!

6 months ago 0 comment
TwitterLinkedin
Uncategorized

How to add additional file types in Wordpress

by Matt Heff 11 months ago
written by Matt Heff

This is the simplest way to give yourself complete freedom to put whatever type of file you want up in wordpress, the standard list of files is far to narrow for most sites that are going to be delivery more than just text as content.

Let’s get straight to it!

Allow Additional File types using wp-admin

This method requires that you have access to the file system and can modify the wp-config file, if you are not able to do this then you are better off using a plugin or modify the functions.php file ( more on that below).

Steps to reliever your frustration

  • Navigate to your WordPress installation directory
  • Make a copy of your wp-admin.php ( just incase)
  • Open wp-admin.php in any editor
  • Add this to the file
define ( ‘ALLOW_UNFILTERED_UPLOADS’ , true );
  • Now save the file into your WordPress directory, flushing the caches may help it take effect quicker.

* Note: Some plugins / themes may have their own file filters in place, but this is guaranteed to work with the core media library.

Allow Additional File types using functions.php / upload_mimes filter.

If you dont have direct access to the file system then this may work for you

  • Navigate to via Appearance -> Theme File Editor
  • Select the functions.php file
  • Add the following code
function my_mime_types($mimes) {
    $mime_types = array(
        'json'     => 'application/json',
    );
    return $mimes;
   }
add_filter('upload_mimes', 'my_mime_types');

define( 'ALLOW_UNFILTERED_UPLOADS', true );  // this is the same setting as we has in wp-config.php in the previous section
  • Save the modifications to the functions.php and you will be good to go!

That is pretty much it for this entry, below is a list of file types you may be interested in these are mostly there to help me catch interest from specific searches for those file types.

I hope this has been helpful for you.

Types of files to allow in WordPress

Documents & Sheets

  • .docx & .doc (Microsoft Word Document)
  • .rtf (Rich Text Format File)
  • .tex (LaTeX Source Document)
  • .log (Log File)
  • .pdf (Adobe: Portable Document Format)
  • .xlsx, .Xls (Microsoft Excel Document)
  • .pptx, .ppt, .pps, .ppsx (Microsoft Powerpoint File)
  • .odt (OpenDocument Text Documet)

Audio Files

  • .wav (WAVE Format)
  • .mp3 (MPEG3 Format)
  • .ogg (OGG Multimedia Container)
  • .m4a (Advanced Audio Coding)
  • .aif ( Audio Interchange File Format)
  • .wma ( Windows Media Audio File)

Image Files

  • .jpeg & .jpg (Joint Photography Experts Group)
  • .psd (Adobe Photoshop Document)
  • .png (Portable Network Graphics)
  • .gif (Graphics interchange Product)
  • .ico (Icon File Extension)
  • .obj (Wavefront 3d Object File)
  • .3ds (3D Studio Scene)
  • .tif (Tagged Image File)
  • .tiff (Tagged Image File Format)

Video Files

  • .wmv (Windows Media Video)
  • .rm (RealMedia File)
  • .flv (Flash Video File)
  • .mpg (MPEG Video)
  • .mp4 (MPEG4 Format)
  • .m4v (Video Container Format)
  • .mov (Quick Time Format)
  • .avi (Audio Video Interleaved Format)
  • .ogv (OGG Vorbis Video Encoding)
  • .3gp (Mobile Phone Video)

Data Files

  • .CSV Comma-Separated Values File
  • .DAT Data File
  • .GED GEDCOM Genealogy Data File
  • .JSON JavaScript Object Notation File
  • .KEY Apple Keynote Presentation
  • .KEYCHAIN Mac OS X Keychain File
  • .PPT Microsoft PowerPoint Presentation (Legacy)
  • .PPTX Microsoft PowerPoint Presentation
  • .SDF Standard Data File
  • .TAR Consolidated Unix File Archive
  • .VCF vCard File
  • .XML XML File

11 months ago 0 comment
TwitterLinkedin

Recent Posts

  • How to read environment variables with Node.js
  • AI – What is Natural Language Processing (NLP)
  • Web3 RPC Nodes
  • Drupal 9 – Custom module development – Simple Module
  • Create a RESTful API in NodeJS

About

About

GetCoding is a blog and tech educational resource specialized in programming, Web3, blockchain and everything in between. Follow us to help you understand and GetCoding!

Stay Connect

Twitter Linkedin Youtube Email

Popular Posts

  • 1

    Writing your first Ethereum Smart Contract

    12 months ago
  • 2

    Create your own JSON.stringify() function

    11 months ago
  • 3

    Introduction to JSON with Examples

    1 year ago
  • 4

    Install Metamask and get Testnet Tokens

    12 months ago
  • 5

    What is an AMM ( Automated Market Maker)

    12 months ago

Categories

  • Blockchain (4)
  • Blog (7)
  • Featured (3)
  • Learning (1)
  • Programming (5)
    • JavaScript (3)
    • NodeJS (1)
    • PHP (1)
  • Technologies (2)
    • Drupal (1)
  • Thought Provoking (1)
  • Tutorial (6)
  • Uncategorized (3)

Recent Posts

  • How to read environment variables with Node.js

    2 months ago
  • AI – What is Natural Language Processing (NLP)

    6 months ago
  • Web3 RPC Nodes

    10 months ago

Featured Posts

  • How to read environment variables with Node.js

    2 months ago
  • AI – What is Natural Language Processing (NLP)

    6 months ago
  • Web3 RPC Nodes

    10 months ago

Subscribe Newsletter

Subscribe my Newsletter for new blog posts, tips & new photos. Let's stay updated!

  • Twitter
  • Linkedin
  • Youtube
  • Email
GetCoding
  • Home
  • Courses
  • Contact