Nikola Brežnjak blog - Tackling software development with a dose of humor
  • Home
  • Daily Thoughts
  • Ionic
  • Stack Overflow
  • Books
  • About me
Home
Daily Thoughts
Ionic
Stack Overflow
Books
About me
  • Home
  • Daily Thoughts
  • Ionic
  • Stack Overflow
  • Books
  • About me
Nikola Brežnjak blog - Tackling software development with a dose of humor
Stack Overflow

Get/Set values of dynamical generated input fields using ng-repeat in Angular

profile for Nikola at Stack Overflow, Q&A for professional and enthusiast programmers
I’m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%). In this category (stackoverflow) of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread here.

My quesiton was:

I have an input field which once filled with a number populates that count of additional input fields. I can’t figure out how to get the values inserted in those generated input fields. Any help is appreciated, and oh, I did try various things and even with angular.element, or solely with jquery, but failed, so any explanation on how to do this is welcome.

Here is my jsfiddle, and the code c/p-ed here:

<div ng-controller="MyCtrl">
    <input type="text" ng-model="cntInputs" placeholder="#Inputs"><br/>

    <input ng-repeat="i in getTimes()" type="text" placeholder="{{i}}" id="input_{{i}}">

    <ul>
        <li ng-repeat="j in getTimes()">
            Inputed value is: {{getValue(j)}}
        </li>
    </ul>
</div>

js:

var myApp = angular.module('myApp', []);


function MyCtrl($scope) {
    $scope.cntInputs = 0;

    $scope.getTimes=function(){
        var a = new Array();

        for(var i=1; i <= $scope.cntInputs; i++)
            a.push(i);

        return a;       
    };

    $scope.getValue = function(id){
        //here get the value of that inserted in the element with the id of "input_" + id
        return angular.element("input_" + id);
    }
}

The answer, by James Kleeh, was:

So add ng-model="inputs[i-1]" to your text field

Then in your controller add $scope.inputs= [];

Then in your getValue function: return $scope.inputs[id-1];

If you make your getTimes function 0 based you wouldn’t need the -1s

Updated your fiddle: http://jsfiddle.net/7MhLd/60/

Books

Storm Front – Jim Butcher

The only quote I liked from the book Storm Front by Jim Butcher:

I made a vampire cry. I felt like a real super hero now. Harry Blackstone Copperfield Dresden, breaker of monster’s hearts.

Stack Overflow

Webkitdotnet unable to load https site

profile for Nikola at Stack Overflow, Q&A for professional and enthusiast programmers
I’m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%). In this category (stackoverflow) of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread here.

My quesiton was:

I am using this webkitdotnet in my C# project. It all went well until I had to use access site with https.

I’ve searched their forum and found few posts about this but none of it solves my problem, so please shed some light on this one. Thx!

edit: Also as mentioned in their threads (also without an answer) I get a “Peer certificate cannot be authenticated with known CA certificates” error when trying to access my server, buthttps://www.google.com works fine.

They also mention the “apple” build which worked fine with ssl (at least so they say), but I can’t find it anywhere…

The answer, by therealmitchconnors, was:

This is a bit of a hack, but you can make webkitdotnet ingore peer ssl errors. WebKitDotNet uses WebKit, which, in turn uses curl, which is responsible for your wonderful ssl error there. curl exposes an option to ignore ssl errors, but neither webkit nor webkitdotnet seem to expose this functionality in their api. However, if you checkout the webkit source code, webkit sets the curl option (CURLOPT_SSL_VERIFYPEER) to false if the value of the environment variable WEBKIT_IGNORE_SSL_ERRORS is set to true.

What this all boils down to is that if you set the environment variable in code before initializing either webkit or webkitdotnet components, webkit will ignore the bad certificate and allow you to navigate to the site (sort of like clicking Proceed Anyway on IE9’s Bad Certificate Warning page).

C++:

setvar("WEBKIT_IGNORE_SSL_ERRORS", "1");

C#:

Environment.SetEnvironmentVariable("WEBKIT_IGNORE_SSL_ERRORS", "1");

If anyone is interested, the webkit source code referenced is in file webkit\Source\WebCore\platform\network\curl\ResourceHandleManager.cpp at lines 65 and 681, currently.

CodeProject, NodeJS

How I built my own testable The Answer To Life The Universe And Everything npm module

TL;DR: In this short tutorial I’ll show you how I built my own testable (Jasmine) npm module which I then published to npm repository. The npm module is here, and the GitHub repository is here.

First, if you haven’t already, set some author info in your terminal:

npm set init.author.name "Nikola Brežnjak"
npm set init.author.email "[email protected]"
npm set init.author.url “http://www.nikola-breznjak.com"

Then, create a new GitHub repository, clone it to your disk and then run npm init. Create a index.js file and copy/paste the following code:

module.exports = function(){
    return 42;
}

Next, install Jasmine by doing:

npm install jasmine-node -g

To test your module, create a folder spec, and a file TheAnswerToLifeTheUniverseAndEverything.spec.js inside it with the following content:

var TheAnswerToLifeTheUniverseAndEverything = require('../index.js');

describe("TheAnswerToLifeTheUniverseAndEverything Suite", function() {
 it("should return a value of 42", function(done) {
 var value = TheAnswerToLifeTheUniverseAndEverything();
 expect(value).toEqual(42);
 done();
 });
});

In order to test your module, run jasmine-node spec/ as shown on the image below:

jasmineTest

In order to publish your npm module you first have to execute the npm adduser command and answer few of the questions, and after that you just have to execute npm publish and you’ll have your npm module listed on npmjs.com:

theAnswerToLifeNPMmodule

Now you can use npm install theanswertolifetheuniverseandeverything to install the module.

If you’re testing this your self and you get an error like “Error: forbidden New packages must have all-lowercase names: TheAnswerToLifeTheUniverseAndEverything” then that means what it says – you must use only lowercase letters when naming your modules.

Books

Scream free parenting – Hal Edward Runkel

My favourite quotes from the book Scream free parenting by Hal Edward Runkel:

Parenting is not about kids, it’s about parents. The greatest thing you can do for your kids is learn to focus on yourself.

We are responsible for our children – this is the biggest lie. We have responsibility to our children.

To be in charge as a parent means inspiring children to motivate them selves.

The definition of insanity is doing the same thing over and over again expecting a different result.

Then importance of giving your children increasing space as they age is far greater than the fear of what they will do with that space.

It is your room, clean it if you will.

Let them close the door. Knock before you come in, and await their answer and if it’s NO, come back later.

Give them money, but then teach them about it.

Let them struggle.

You can’t? Man, that sucks, what have you tried so far? After the response ask: so what are you going to do about it?

This is what I think, what do you think?

Wow, you’re bored. That stinks. I hate it when I’m bored too. What are you going to do about it?

After 10 minutes in the car – are we there yet? Wow it looks like you really don’t want to be in the car right now. You know what, me neither – and for whole next two hours! What are we going to do about it?

The very act of homework hovering makes a problem even worse.

Learning from our mistakes is the highest form of education possible.

I told you so – don’t use this.

Integrity. It’s very important for your children to believe that what you say is what you honestly believe and what you will actually do.

Broken body makes it extremely difficult to provide for and protect your children. Your health is your first responsibility. Your self-respect is important, as well as your marriage – your marriage is more important than your kids. Take care of yourself in order to benefit others.

The only way to change the pattern is focusing on your part in it, and then do something different. Don’t wait for others to change – you change and the rest will follow.

NodeJS, Servers

How to host multiple Node.js applications with multiple different domains on one VPS with Nginx

First things first, if you bought your domain elsewhere then you first need to point that domain to your server. You basically have two options here

  • installing, setting up and running a DNS on your VPS and pointing the DNS (from the control panel where you bought a domain) records to your VPS
  • setting your DNS Zone file A record (in the control panel where you bought a domain) to the VPS IP
  • this posts explain what are the pros and cons

Now, if you do that for multiple domains they will all point to your server’s IP and show the same thing, essentially the thing which is running on port 80 (and that, in our main Nginx installation, will be a default Nginx welcome screen). If you have multiple Node.js applications, which are running on different ports, and you want to pinpoint the domains to that particular applications, then this is where the Nginx comes in so that it takes the requests for each domain and routes it to an appropriate port where the appropriate Node.js application is running. Basically, what Nginx does in this case is called reverse proxying.

Useful links:

  • this StackOverflow question
  • http://blog.grabeh.net/Digital-Ocean-VPS-Nginx-Express-apps
  • https://www.digitalocean.com/community/tutorials/how-to-host-multiple-node-js-applications-on-a-single-vps-with-nginx-forever-and-crontab
  • http://ludovf.net/blog/configure-nginx-to-proxy-virtual-hosts-to-apache/
  • http://kbeezie.com/apache-with-nginx/
  • https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-virtual-hosts-server-blocks-on-centos-6

Use forever:

  • npm install forever -g
  • forever start –spinSleepTime 10000 yourApp.js
  • if the app crashes, forever starts it up again

Lets start:

  • sudo vim /etc/nginx/conf.d/example.com.conf:
    server {
        listen 80;
    
        server_name your-domain.com;
    
        location / {
            proxy_pass http://localhost:{YOUR_PORT};
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }
  • to reference multiple domains for one Node.js app (like www.example.com and example.com) you need to add the following code to the file /etc/nginx/nginx.conf in the http section: server_names_hash_bucket_size 64;
  • but, as it always is, the upper statement didn’t work for me, so I ended up using this solution from StackOverflow:
    server {
        #listen 80 is default
        server_name www.example.com;
        return 301 $scheme://example.com$request_uri;
    }
  • sudo service nginx restart

Forever keeps your application running when it crashes but if VPS is rebooted it won’t start anymore. Simple cronjob solves this issue. Create starter.sh in your application’s home folder and copy the following code:

#!/bin/sh

if [ $(ps -e -o uid,cmd | grep $UID | grep node | grep -v grep | wc -l | tr -s "\n") -eq 0 ]
then
export PATH=/usr/local/bin:$PATH
forever start --sourceDir /path/to/your/node/app main.js >> /path/to/log.txt 2>&1
fi

where main.js is your application’s main script. Add to crontab the following line: @reboot /path/to/starter.sh.

Servers

Installing MEN NPP stack (MongoDB, Express, Node.js, Nginx, PostgreSQL, PHP) on CentOS 7 from scratch

I’ve been playing with my VPS that I bought from WeLoveServers.net and I previously wrote about how to set up your VPS from scratch. Here I’ll show just brief commands for installing a MEN NPP stack (MongoDB, Express, Node.js, Nginx, PostgreSQL, PHP) on my CentOS machine.

Nginx

  • high performance web server that is much more flexible and lightweight than Apache
  • SELinux – if you run into issues change the SELinux mode to permissive or disabled
  • sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
  • sudo yum install nginx
  • sudo systemctl start nginx.service or sudo service nginx start
  • sudo systemctl enable nginx.service (no alternative with sudo service)
  • default server root directory /usr/share/nginx/html defined /etc/nginx/conf.d/default.conf
  • server blocks (known as Virtual Hosts in Apache) are created in /etc/nginx/conf.d. Files that end with .conf in that directory will be loaded when Nginx is started
  • main Nginx configuration file is located at /etc/nginx/nginx.conf
  • sudo vi /etc/nginx/conf.d/default.conf looks like:
    server {
        listen       80;
        server_name  localhost;
    
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }

    change to:

    server {
        listen       80;
        server_name  server_domain_name_or_IP;
    
        root   /usr/share/nginx/html;
        index index.php index.html index.htm;
    
        location / {
            try_files $uri $uri/ =404;
        }
        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root /usr/share/nginx/html;
        }
    
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
  • sudo systemctl restart nginx
  • sudo nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful

Node.js

  • just a side note for Mac: brew install node
  • sudo yum install epel-release
  • sudo yum install node
  • sudo yum install npm

Express

  • npm install express

PHP

  • sudo yum install php php-mysql php-fpm
  • sudo vi /etc/php.ini – in order to secure it do cgi.fix_pathinfo=0
  • sudo vi /etc/php-fpm.d/www.conf set line to listen = /var/run/php-fpm/php-fpm.sock
  • sudo systemctl start php-fpm
  • sudo systemctl enable php-fpm.service to start from boot

 

PostgreSQL

  • exclude the CentOS version of postgres in order to get the most recent version from the project’s website
  • sudo vim /etc/yum.repos.d/CentOS-Base.repo
    At the bottom of the [base] and [updates] sections, add a line that excludes the postgres packages: exclude=postgresql*
  • Find the link for your version of the OS here
  • curl -O http://yum.postgresql.org/9.4/redhat/rhel-7-x86_64/pgdg-centos94-9.4-1.noarch.rpm
  • rpm -ivh pgdg*
  • yum list postgres*
  • install the -server package: sudo postgresql94-server.x86_64
  • sudo /usr/pgsql-9.4/bin/postgresql94-setup initdb
  • chkconfig postgresql-9.3 on
  • service postgresql-9.3 start
  • postgres creates a user and a database called postgres
  • sudo su – postgres
  • psql
  • \?: Get a full list of psql commands, including those not listed here
  • \h: Get help on SQL commands. You can follow this with a specific command to get help with the syntax
  • \q: Quit the psql program and exit to the Linux prompt
  • \d: List available tables, views, and sequences in current database
  • \du: List available roles
  • \dp: List access privileges
  • \dt: List tables
  • \l: List databases
  • \c: Connect to a different database. Follow this by the database name
  • \password: Change the password for the username that follows
  • \conninfo: Get information about the current database and connection
    CREATE TABLE new_table_name (
        table_column_title TYPE_OF_DATA column_constraints,
        next_column_title TYPE_OF_DATA column_constraints,
        table_constraint
        table_constraint
    ) INHERITS existing_table_to_inherit_from;

     

MongoDB

  • Instructions from here
  • sudo vim /etc/yum.repos.d/mongodb.repo:
    [mongodb]
    name=MongoDB Repository
    baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
    gpgcheck=0
    enabled=1
  • sudo yum install -y mongodb-org
  • to prevent unintended upgrades, add to /etc/yum.conf file:
    exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools
  • sudo service mongod start
  • sudo chkconfig mongod on

 

Servers

How to set up your own VPS from scratch

I bought a VPS on a sale on WeLoveServers.net for a crazy 48$ a year with 2GB of RAM. If you know a cheaper one, please share it in the comments. Anyways, since this is a barebones setup, I had to set it up myself and this is how I did it.

I followed a great tutorial from DigitalOcean: https://www.digitalocean.com/community/tutorials/initial-server-setup-with-centos-7

Login

  • login as root
  • change root’s password with passwd command
  • add new user – useradd myuser
  • change user’s password – passwd myuser
  • on CentOS 7, users who belong to the “wheel” group are allowed to use the sudo command: gpasswd -a myuser wheel or use a normal route of editing with /usr/sbin/visudo and add a line leuser ALL(ALL) ALL
  • ssh-keygen – you get id_rsa and id_rsa.pub files which you upload (only public one OFC!) to .ssh/ folder in your home directory (you should do chmod 700 .ssh after mkdir-ing it)
  • create file authorized_keys and paste the contents of the id_rsa.pub file in it and restrict the access to it by doing chmod 600 .ssh/authorized_keys

SSH settings

  • sudo vi /etc/ssh/sshd_config
    • Port 25000
      Protocol 2
      PermitRootLogin no
      UseDNS no
      AllowUsers myuser
  • systemctl reload sshd.service or service sshd restart

Firewall

  • my version of CentOS ships with iptables, but in the article he works with a firewall called firewalld (yum install firewalld to install it)
  • lock down everything that you do not have a good reason to keep open
  • sudo systemctl start firewalld
  • uses the concept of “zones” to label the trustworthiness of the other hosts
  • sudo firewall-cmd –permanent –add-service=ssh
  • if you use a different port for SSH then
    • sudo firewall-cmd –permanent –remove-service=ssh
      sudo firewall-cmd –permanent –add-port=4444/tcp
  • sudo firewall-cmd –permanent –add-service=http
  • sudo firewall-cmd –permanent –add-service=https
  • sudo firewall-cmd –permanent –add-service=smtp
  • all the services that you can enable: sudo firewall-cmd –get-services
  • list exceptions: sudo firewall-cmd –permanent –list-all
  • sudo firewall-cmd –reload
  • start firewall at boot: sudo systemctl enable firewalld

Timezone

  • sudo timedatectl list-timezones
  • sudo timedatectl set-timezone Europe/Zagreb
  • confirm the change has been done: sudo timedatectl

NTP

  • sudo yum install ntp
  • sudo systemctl start ntpd
  • sudo systemctl enable ntpd

swap

  • allows the system to move the less frequently accessed information of a running program from RAM to a location on disk, especially useful if you plan to host any databases on your system
  • amount equal to or double the amount of RAM on your system is a good starting point
  • sudo fallocate -l 4G /swapfile
  • sudo chmod 600 /swapfile
  • sudo mkswap /swapfile
  • sudo swapon /swapfile
  • in case after the last command you get an error like this: “swapon: /swapfile: swapon failed: Operation not permitted“, that basically means that you’re most probably on openvz and that you can’t create a swap file (more on serverfault.com)
  • if you didn’t get an error then do: sudo sh -c ‘echo “/swapfile none swap sw 0 0” >> /etc/fstab’ to start it at boot

fail2ban

  • it scans through log files and reacts to offending actions such as repeated failed login attempts
  • EPEL (Extra Packages for Enterprise Linux)
  • wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-2.noarch.rpm
  • sudo rpm -ivh epel-release-7-2.noarch.rpm
  • sudo yum install fail2ban
  • default configuration file at /etc/fail2ban/jail.conf but copy it
    cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
  • vi /etc/fail2ban/jail.local
  • sudo chkconfig fail2ban on
Books

Strong fathers strong daughters – Meg Meeker

My favourite quotes from the book Strong fathers strong daughters by Meg Meeker:
Believe it or not, the most important factor for girls growing up into competent, well-adjusted woman is a strong father would conservative values.
Don’t let your daughter have a TV or computer in her room. Safe TV for family time – when you or your wife can decide what to watch.
Your daughter doesn’t want to see you as equal, she wants you to be her hero. Someone who is wiser and steadier and stronger than she is.
Authority is not a threat to your relationship with your daughter – it is what will get you closer with your daughter and what will make her respect you even more.
Lead by example and don’t back down from your principles!
Women in magazines aren’t the best role models. People who judge everyone on their looks probably have terrible self-esteem issues. It’s not important how thin someone is but what her character is.
Don’t comment frequently on how she looks. Don’t comment on your own needs the diet. Appearance is not a high-priority – what matters is her, and not what she wears. Give her attention naturally. If you talk about something often she will follow.
Be confident. Defend her and be supportive. And don’t back away from hugging her. Don’t allow fashion trends mold your daughter – seven-year-old learns from commercials that being sexy is good – you have to intervene.
Teach her to wait!
Teach self-respect early. At every age tell her her body is special and that she needs to keep it special.
Don’t let your daughter grow up to be a victim of life. Too much of our popular culture teaches us to love victims. So, we create people who are helpless, incapable and terribly needy. But you, as a father, can prevent that. You can teach your daughter to do, not just want. Action engages the will and gives energy and momentum. Action means that your daughter will know that she, not others, will determine her fate.
Constant pursuit of more leads to dissatisfaction. When we realize that we don’t need more, then we can be happy. Contempt comes from who we are and what we have today.
It is a great strength knowing that if you lost every material possesion that you would still have life worth living for by sharing it with your loved ones.
To be a good father is to be a good instructor about God.
My dad has a really strong faith. That’s why he got up every morning to pray and read the Bible. Sometimes he would talk about God, but mostly he would like to just live what he read in the Bible. I was happy because I knew he prayed for me.
There is a void in every man’s heart that only God can fill.
Don’t leave your teenage daughter shopping alone because it only makes it easier for the pop-culture to sell them shit.
Search for that something, but the more we search for it the more distant it becomes. Because what you’re searching for sits right there. It’s not your job, or more money, your hobbies or more sex – it’s your family, your children, your spouse and God.
NodeJS

How to get all of your own Facebook posts and parse them with JavaScript

So, first you need to download all the data that Facebook has stored for you (well, and what it’s willing to give you :)). You can do that by going to https://www.facebook.com/settings when you’re logged in, and you will see the following screen:FacebookDataDownload2

Here you can just click on the Download a copy of your Facebook data and after they process your request you will get the download link on your email.

The download package looks contains 3 folders (videos, photos, html) and a index.html file. In order to take a look at your posts you have to open up the html folder where you’ll find the file named wall.htm. This is a simple HTML file whose contents is put inside the <div class=”comment”> elements.

In order to parse this with JavaScript (you’ll need to have Node.js installed) you can use the following snippet:

function getMatches(string, regex, index) {
    index || (index = 1); // default to the first capturing group
    var matches = [];
    var match;
    while (match = regex.exec(string)) {
        matches.push(match[index]);
    }
    return matches;
}

fs = require('fs');
fs.readFile('wall.htm', 'utf8', function(err, data){
	if (err){
	    return console.log(err);
	}

	var regex = /comment">([\s\S]*?)<\/div>/g
	var matches = getMatches(data, regex, 1);

	var output = '';
	for(var i=0; i<matches.length; i++){
		output += matches[i] + '\n';
	}

	fs.writeFile('parsed.txt', output, 'utf8', function(){
		console.log('done!');
	});
});

What this does is that it reads the wall.html file and outputs just simple text of the posts in a parsed.txt file.

Few of the challenges here we’re the multiline regex matching which I solved with this link on StackOverflow, and another one was getting access to matched groups of a regex in JavaScript.

Page 42 of 51« First...102030«41424344»50...Last »

Recent posts

  • Discipline is also a talent
  • Play for the fun of it
  • The importance of failing
  • A fresh start
  • Perseverance

Categories

  • Android (3)
  • Books (114)
    • Programming (22)
  • CodeProject (35)
  • Daily Thoughts (77)
  • Go (3)
  • iOS (5)
  • JavaScript (127)
    • Angular (4)
    • Angular 2 (3)
    • Ionic (61)
    • Ionic2 (2)
    • Ionic3 (8)
    • MEAN (3)
    • NodeJS (27)
    • Phaser (1)
    • React (1)
    • Three.js (1)
    • Vue.js (2)
  • Leadership (1)
  • Meetups (8)
  • Miscellaneou$ (77)
    • Breaking News (8)
    • CodeSchool (2)
    • Hacker Games (3)
    • Pluralsight (7)
    • Projects (2)
    • Sublime Text (2)
  • PHP (6)
  • Quick tips (40)
  • Servers (8)
    • Heroku (1)
    • Linux (3)
  • Stack Overflow (81)
  • Unity3D (9)
  • Windows (8)
    • C# (2)
    • WPF (3)
  • Wordpress (2)

"There's no short-term solution for a long-term result." ~ Greg Plitt

"Everything around you that you call life was made up by people that were no smarter than you." ~ S. Jobs

"Hard work beats talent when talent doesn't work hard." ~ Tim Notke

© since 2016 - Nikola Brežnjak