Latest Event Updates

node notes: require() caches module.exports

Posted on Updated on

if you are importing a file that instantiates a new object

when you import the file again as a 2nd object you would expect … a new object

but your 2nd variable will be the same object as the first variable, even though a “new” constructor was used. why?

Node caches module imports , it wont load the file again so no new instance!!!

example:

// file greet3.js
function Greetr(){
  this.greeting=”Hello World from greet3″
  this.greet=function(){console.log(this.greeting)}
}
module.exports = new Greetr();

 

//app.js
var greet3 = require(“./greet3.js”);
greet3.greet();
greet3.greeting = “NEW GREETING!!!!”
var greet3a = require(“./greet3.js”);
greet3a.greet();
//console.log
Hello World from greet3
NEW GREETING!!!!
how to resolve??
in the file being exported, greet3.js, send the constructor not the new instance
//greet3.js
module.exports = Greetr;
//app.js
var Greet3a = require(“./greet3.js”);  //you capitalize greet3a to tell its constructor
var grtr = new Greet3a();

Node notes : on require with no js file in local path

Posted on

if you require, for example  “greet”, and greet.js is not found, node will look for folder called “greet” and then look for a file called index.js

 

 

Ubuntu: Install Visual Studio Code via apt-get

Posted on Updated on

Why fiddle with downloading from the browser when apt-get will do?

sudo add-apt-repository -y "deb https://packages.microsoft.com/repos/vscode stable main"
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EB3E94ADBE1229CF
sudo apt update
sudo apt -y install code

If need upgrade

sudo apt -y upgrade
sudo apt -y dist-upgrade

great answer from zurfyx

https://askubuntu.com/questions/616075/how-do-i-install-visual-studio-code

 

 

Errors encountered on my way to install Node.JS v8 on ubuntu

Posted on

when running apt-get install nodejs

You might want to run ‘apt-get -f install’ to correct these:
The following packages have unmet dependencies:
libgail-3-0 : Depends: libgtk-3-0 (= 3.20.9-1ubuntu2) but 3.18.9-1ubuntu4 is to be installed
nodejs : Depends: libuv1 (>= 1.6.1) but it is not going to be installed
python3-gi-cairo : Depends: python3-gi (= 3.22.0-1) but 3.20.0-0ubuntu1 is to be installed E: Unmet dependencies. Try ‘apt-get -f install’ with no packages (or specify a solution).

to fix:  apt-get -f install

When trying to install nodejs again will only allow for node-legacy (v4)

to fix: 

# Using Ubuntu
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs

 

finally at v8 locally

 

io.emit vs socket.emit

Posted on

io.emit lets the server send an event to all the connected clients at once. This is useful when you need to send some data to everyone. This is something we want everyone to be able to see.

socket.emit sends an event to a single socket. This is great when you need to send an event to a single user. Example: This is why we use socket.emit when sending out a greeting message. It only needs to go to that one user.

 

socket.io notes

JavaScript Classes review

Posted on Updated on

Some basic notes

// *****START CLASS***********************************************

//The constructor function is called automatically when an instance of the class is created.
// a special method for creating and initializing an object created with a class.
// There can only be one special method with the name “constructor” in a class.
class Person {
constructor (name,age) { //this is a function
// console.log(name,age)
this.name=name;
this.age=age;
}
//methods
getUserDescription(){return `${this.name} is ${this.age} year(s) old`}
}

// *****END CLASS***********************************************

var me = new Person(“Andrew”,25); //arguments are passed to constructor function
// console.log(“this.name”, me.name)
// console.log(“this.age”, me.age)
var description = me.getUserDescription(); // Andrew is 25 year(s) old
console.log(description);

Location object

Posted on Updated on

Contains info about the URL

Part of the window object, also accessible by document object

Can access in dev tools by typing location

Items in location

  • search. : that is the query string 
  • pathname 
  • origin 
  • href 
  • port

See MDN for more
Examples: access seatch use window.location.search
See stevenbenner jQuery deparam for deconstruction of params  or go here 

Example deparam after loading function 

JQuery.deparam(window.location.search)

Continuous posting on blog

Posted on

Was watching a YouTube video by simple programmer where he pointed out that one should keep blogging and putting down their thoughts.  It shows that you have been working on a particular tech.

That’s was a good point. It reinforces your own learning and can help others who are also looking for information. When I was learning VMware I wrote tons of articles that were used by others. I would even surprise myself and find my own articles when looking for an answer.

Downloaded the WordPress app and will arrive for more as we all should 

favicon.ico Failed to load resource: the server responded with a status of 404 (Not Found)

Posted on Updated on

One of those things that only rears it’s head once you place a project into production.

I kept getting this error even though I have not specified any favicon in my code.

Now I ask myself, what is a favicon? According to wikipedia a favicon is that little image that shows up next to a web address. Accoding to this stackoverflow link, chrome will request favicon 3 times for each IFRAME

Following the SO link, yahoo says “The favicon.ico is an image that stays in the root of your server. It’s a necessary evil because even if you don’t care about it the browser will still request it, so it’s better not to respond with a 404 Not Found

Options:

  1. Create a small 1kb ico to satisfy the browser
  2. use this to override:
    <link rel="icon" href="data:;base64,iVBORw0KGgo=">
    

What is the end goal? Musings on programming.

Posted on

I a doing FreeCodeCamp’s Build a Weather app for the 2nd time. (maybe 3rd? who knows)

This time I thought I would get fancy, try to get the location from HTML5 first, then if an error, get the location from a IP geolocation service.

Well, I am finding that some functions don’t wait for the return of data from another, and rather than having an orchastrator function I would need to chain the functions together.

example:

Original:  master() call geoHTML5; if error master() call geoIP;

now: master() call geoHTML5 call geoIP() call….

Now I could just forgo HTML5 geolocation since IP geolocation works and actually has better data than HTML5 gelocation.

What is my end goal? To finish the project? Then ip geolocation would be the right choice, dont waster time on other things.

But what is my REAL end goal? TO get better at programming and learn the ins and outs of javascropt. That means I need to deal with my original plan and found out how to implement it..

What is the end goal? I’ve already done a weather app. The goal is to get better at programming.