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
Quick tips, Windows

Grep GUI on a Windows machine with AstroGrep

I had to find some string inside one of the files, and I couldn’t remember in which file it was, classic 🙂

In linux, simple grep command would help, but on Windows there’s no such thing, or at least I thought… After some googling I came up with AstroGrep and I think it’s awesome, and it also comes with a GUI if you fancy that:

AstroGrep

You can download it freely from their site.

Stack Overflow

NodeJS code executes differently when run via browser and when run via curl in terminal

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 the following simple NodeJS code:

var http = require("http");

http.createServer(function(request, response){
    response.writeHead(200);
    setTimeout(function(){
        response.write("This printed 3 secs later, ait?");
        response.end();},3000);

    response.write("This will be printed before.\n");}).listen(8080);

If I run the script with node scriptname.js and then access it via curl in terminal like this:

curl http://localhost:8080

I get an output as expected, first it prints This will be printed before., then after 3 seconds it prints This printed 3 secs later, ait?.

However, when I open http://localhost:8080 in my browser (newest versions of Chrome, Firefox) page loads for 3 seconds and it prints the text This will be printed before. This printed 3 secs later, ait? all at once. Why does this happen and how could I make the same behavior in the browser?

edit: So, as Ken stated in his answer

…this is simply due to the behavior of the browser rendering engine to render the contents. The rendering engine cashes the contents until response.end();

and advised to go and check out Socket.IO, I came up with this working example which uses expressand Socket.IO:

//timeoutTest.js
var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io').listen(server);

server.listen(8080);

app.use(express.static(__dirname +'/'));

app.get('/',function(req, res){
   res.sendfile(__dirname +'/timeoutTest.html');});

io.sockets.on('connection',function(client){
   client.emit('msg',"This prints right now on connection.");

   setTimeout(function(){
      client.emit('msg',"This prints out after 3 secs.");},3000);});
//timeoutTest.html
<!DOCTYPE html><html><head><script src="/socket.io/socket.io.js"></script><script src="jquery.js"></script><script>
    $(document).ready(function(){var server = io.connect('http://localhost');
        server.on('msg',function(data){
            $('body').append(data +"<br/>");});});</script></head><body></body></html>

 

The answer, by Ken OKABE, was:

I think this is simply due to the behavior of the browser rendering engine to render the contents. The rendering engine cashes the contents until response.end(); arrives, then renders whole.

Basically, HTML contents in the browser are never updated automatically by incremental server pushsuch as response.write. You must pull data from the server to the browser client by Ajax and DHTML/js technique. curl in terminal is completely different story in terms of the mere output of the node server.

If you seek more interactive behavior between node server and browser client, if you want server push feature, websocket is the way to go, and also investigate node stream stuff. Socket.IO is famous, and for node stream https://github.com/substack/stream-handbook should be interesting to you.

I personally do this on my own project: http://kenokabe.github.io/MarkdownLive/. I write stuff using Markdown and needed a streaming preview, so I created by myself. The preview screen is a Browser HTML page, and the HTML contents rendered and updated incrementally in streaming manner.

MEAN

MEAN T-shirt graphic, presentation and video

Since I’m all into MEAN stack lately, I made a custom graphic for a MEAN T-shirt. Also, I made the presentation and a talk for hack.hands().

I’m giving the image here freely without any restrictions, yada, yada, use it whatever way you like, just don’t sue me if someone will tell you you’re a mean person :P. You can send the image to your local T-shirt makers, or you can order it from my SpreadShirt store.

The T-shirt looks like this on me:

IMG_2617

Desktop background version can be downloaded here (Right click, Save Image As):MEAN_1_black

And a transparent PNG version ideal for a black T-shirt can be downloaded here (Right click, Save Image As):

MEAN_1

I got a t-shirt made at a reasonable price here in Croatia at t-shirtmania.hr.

The video of the talk in full on youtube below, and the two posts in a series of getting started on a MEAN stack are here:

  • https://hackhands.com/how-to-get-started-on-the-mean-stack/
  • https://hackhands.com/delving-node-js-express-web-framework/

Stack Overflow

How to pass GET parameters to jsFiddle

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:

How to pass GET parameters to jsFiddle? I tried http://jsfiddle.net/mKwcF/?id=123 but all I get ishttp://fiddle.jshell.net/mKwcF/show/

My example js is simple on a given link above:

alert(window.location.href);

The (updated) answer, by Jack Miller, was:

As of October 2014 it is a little more complicated than it used to be:

If your jsfiddle url is:

http://jsfiddle.net/u7G7n/41

Some code to make stackoverflow happy. Ignore this.

use instead (including some url parameters):

http://fiddle.jshell.net/u7G7n/41/show/light/?lat1=52&lng1=9&lat2=50&lng2=1

AND send the same url (including parameters) as referer. You can use Referer Control as Chrome plugin: https://chrome.google.com/webstore/detail/referer-control/hnkcfpcejkafcihlgbojoidoihckciin

And configure like this: enter image description here

Now the second link (including parameters) should work.

Stack Overflow

phpMyAdmin Class ‘PMA_Message’ not found in /usr/share/ phpMyAdmin/libraries/ Error.class.php

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 just got this error (no errors in phpMyAdmin for the last 2 years):

PHP Fatal error: Class ‘PMA_Message’ not found in /usr/share/phpMyAdmin/libraries/Error.class.php on line 24

I tried searching the net and links like

  • http://gnuwhatimsaying.com/phpmyadmin-error-class-pma_message-not-found/ (changing the ownership of /var/lib/php/session/ folder to apache:apache – it’s already set like that)
  • http://rakesh.sankar-b.com/2012/05/20/phpmyadmin-pma-message-class-not-found/ (quote: “I got a weird idea to see if the space is available for PHP to create sessions – gotcha, that is where my problem was. I cleaned up some unnecessary items and all went well.” – to be honest I don’t know what the author meant by this)

but none solved my issues, and strangely enough – these are only two links which pop up in the first 50 links of google search for this error. So, please shed some light on this.

Btw, the site which is hosted on this server works fine, DB works fine (mysql, login via SSH).

The answer, by Chris Muench, was:

I fixed this issue by restarting apache.

My comment on this answer was:

Thanks, in my case the reload was enough (service httpd reload)

Stack Overflow

Regular expression to match sets of numbers that are not equal nor reversed

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 referring as a follow up on this question here: Regular expression to match two numbers that are not equal

Now my other question scenario is this:

P121324 - T  
P1212 - F - we got this covered in the message on link above (no same "sets")
P1221 - F - now new restriction -not even the reversed digits 12-21

But, the problem now is that the P string can be very long! – like this:

P121315162324

please notice this is ok since the “sets” are:

12131415162324

Now, I could make this in the code (PHP) by checking if there are repetitions, but I was wondering if this can be done with single regex command?

The answer, by Mark Byers, was:

Try this:

^P(?:([0-9])(?!\1)([0-9])(?!(?:..)*(?:\1\2|\2\1)))*$

If you want the digits to be restricted to [1-6] like in your previous question then change [0-9] to [1-6].

See it working online: rubular


Here is a breakdown of the regular expression:

^          Start of string/line.
P          Literal P
(?:<snip>) Non-capturing group that matches a distinct pair of digits. See below.
*          Zero or more pairs (use + if you want to require at least one pair).
$          End of string/line.

Explanation of ([0-9])(?!\1)([0-9])(?!(?:..)*(?:\1\2|\2\1)) – match one pair:

([0-9])    Match and capture the first digit. Later refered to as \1.
(?!\1)     Negative lookahead. The next character must not be the same as \1.
([0-9])    Match and capture a digit. Later refered to as \2.
(?!<snip>) Negative lookahead. Check that the pair doesn't occur again.

Explanation of (?:..)*(?:\1\2|\2\1) – try to find the same pair again:

(?:..)*       Match any number of pairs.
(?:\1\2|\2\1) Match either \1\2 or \2\1.
Stack Overflow

Best practice for C# calling PHP which then queries the database

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:

For some reason I have to have a windows client application (written in C#) which communicates with the PHP files that are on my server. Windows application can’t be allowed to have SQL queries in the code because of the possible disassembling of the exe file. This is the main reason why this approach is used.

Basically it looks like this: from windows client i call getResult.php which then opens the connection to the database, queries the database, returns the result to the client and closes the database connection. Therefore windows client doesn’t have any code for querying the database, it just has calls to the PHP file.

My several questions follow:
1. What is the best way to send request from c# code to the PHP file? (Cause I need to send this php file some parameters like ID, etc… -> I know I can do it with GET like this getResult.php?id=123456, but is this same possible with POST? And also, one question: how to do this in code? http requests or?)

2.Since every time I call the PHP file (there will be more files which I will call, like getResult.php, getStatus.php, etc…) I will somehow need to send login information to that PHP file with which that PHP will query the database. My question here is how to do this securely, and plus: is it maybe somehow possible to call something like doLogin.php and send the login username and password one time, and after that call this (and all other) php files without the need to send the login information as a parameter to the function. I know I can use PHP sessions when the whole application is on the server, but the main difference here is that I am only calling some files, executing them and closing the connection.

My main question is: is this ok from conceptual point of view or are there any commonly known concepts for this, for which I don’t know about – please advise I’m willing to learn. I did some research and do believe this might have to be done with web services approach, but please do reply your thoughts on this.

The answer, by Steve Mayne, was:

Your PHP code is effectively serving as a RESTful data-access API. Run your PHP on a webserver over SSL (HTTPS) so that all your comms are encrypted.

You could either use trusted certificates to authenticate the client, or if you require different access levels, submitting a username/password to get an authorisation token for the data-access requests is not a bad idea.

Here is an example from Yahoo!: http://developer.yahoo.com/dotnet/howto-rest_cs.html

But, another good answer was from user Tom Glenn:

For a simple GET you can do:

var webClient =newWebClient();
webClient.DownloadString("http://someurl.com/somescript.php");

You could then return perhaps an XML or JSON formatted response from the PHP script? You can use WebClient for POST too.

As for the login, you can do that too. I do a similar thing in one of my applications. We send the login details to the script (ASP.NET not PHP) and the ASP page returns an XML response telling the C# app whether or not it was successful – the application can then decide whether it is allowed to continue or not.

Servers

VPS with 1GB of RAM for 19$ per year!?

The guys at WeLoveServers.com are having an anniversary sale. And a sale it is indeed! If someone knows of a better option please share it in the comments!

In order to use the sale, visit their site through my aff link: http://core.weloveservers.net/aff.php?aff=584 and after this click on any of the sale links below.

They’re offering the following two options:

  • LEB 1GB – the link to the sale is here
  • LEB1
  • LEB 2GB – link to the sale is here
    LEB2

So, basically, you get 1GB of RAM VPS for less than 19$ a year, and you get 2GB of RAM VPS for 48$ a year. Important thing to note is that I spoke with their sales representative and he told me that the price after this first year stays the same!?

Settings and additional options which you can set during the signup are:

WLSsettings

You can buy additional RAM, storage, bandwidth, IP addresses, and even set a location of your server (TX, CA, NY, FL, UK). True, originally you don’t get any admin panel, but they offer a cPanel for 14.95$ a month if you really need one.

Anyways, I got myself one of the LEB 2GB and so far I’m very pleased with it and I have to say kudos to the customer service.

The basic panel looks like this:

wlsPanel2

We’ll see how this goes in the future – of which I’ll update you on this blog.

Happy testing!

Stack Overflow

How to serialize everything but the checkbox elements in jQuery.serialize()?

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 if there is a way to serialize all form elements using serialize() function except the checkboxes?

The answer, by Simeon, was:

The .serialize() method can act on a jQuery object that has selected individual form elements, such as<input>, <textarea>, and <select>.

You could do this:

var s = $('#post-form').find('input, textarea, select').not(':checkbox').serialize()
Hacker Games, JavaScript

JavascriptBattle

Lately I’ve been playing at JavascriptBattle, which is

a fun and engaging web application intended to make artificial intelligence (AI) design accessible to all. Every day, your code will be pulled down from your “hero-starter” repository on Github. Your code (along with every other user’s code) will be run daily, behind the scenes, in our game engine.

Basically, you write JavaScript code for your hero in order to instruct him how to behave when for example his health drops to a certain level, or when enemies are close, etc. and then you participate in a daily battle. They have various stats, and as it turns out I’m currently on position #3 for average damage dealt, yay me!

JavascriptBattle

 

Anyways, it’s cool so feel free to check it out and take me down from the pedestal :P. If you happen to like these “hacker games” then do checkout the one I made for my university project back in the old days 1415131129_smiley-evil.

Page 43 of 51« First...102030«42434445»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