Sunday, November 10, 2013

Need to access your node.js app via HTTPS?

Recently I used several Javascript libraries/frameworks to develop some tools to boost my performance of setup environment and testing our product. Also it helps me to sharp my Javascript skills. I feel like my Javascript skill is catching up with my Java skill. And most importantly, I feel like writing Javascript is much more fun than writing Java.

Here is a simple note on how to serving content via node.js with HTTPS and make browser accept the certificate automatically.

First, use this commend from this The Most Common OpenSSL Commands to create your private key and certificate:
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout privateKey.key -out certificate.crt

Second, install the certificate.crt to Keychain in Mac (You may need other solution for other OS). Follow the instructions in this Google Chrome, Mac OS X and Self-Signed SSL Certificates.

Last, I'm using the Connect middleware lib for node.js. To serve via HTTPS, you need something like:
var connect = require('connect'),
    http = require('http'),
    https = require('https'),
    fs = require('fs');

var app = connect().
  // ...
  // configure app accordingly

var options = {
  key: fs.readFileSync('privateKey.key'),
  cert: fs.readFileSync('certificate.crt')
};
https.createServer(options, app).listen(9999);

That's it. Once you start your server via node, you should be able to access your content via HTTPS.

Sunday, October 6, 2013

So many object-oriented design principles

In my recent project, I thought and discussed deeply about what object-oriented we should use for our project.

I realized there are so many of them. There is not one which is absolute right or wrong. And sometimes, more annoying, some of them may even look like conflicting to each other. Like "Tell don't ask" vs. "Single Responsibility".

Previously, I think they do conflicts with each other. But after a while, I realized that not really. "Single Responsibility" doesn't mean there could be only one method. It could be "manage order entity" as the single responsibility of an OrderManager class which has multiple methods like create/update/delete, etc.

I also get challenged a lot by people about the "Composite over inheritance" rule. I personally like to use inheritance to provide a default implementation of a plugin or strategy, and allow the really implementation to inherit from the default one and only override the methods they interest in.

Almost anytime, people will just say don't use inheritance, use composite, when reviewing my code. And of course, we could use composite to implement it. But if inheritance works well for it, why do we literally want zero inheritance and only prefer composite?

If we check "Composite over inheritance" from Wikipedia, the example is basically factor out the functionality of a class into a couple of features or strategies, like Flyable, Quackable, etc. But look closely, when providing the implementations of these features/strategies, we already use "implements", since each of them is just a simple interface with just one method. But what if it's not that simple, and has more than one methods? We may want to provide a default implementation of methodA and methodB, and let the other implementation to selectively override either methodA or methodB or both. Here we will need inheritance again. In this case, I don't see a problem with inheritance at all.

So, we should not blindly follow any OO design principles, try them by yourselves and summarize which principle works well in which cases and works not well in which other cases. There is always a thing called refactory which could help you to get to a better design/implementation as long as you have good test coverage.

Simply saying which OO design principle is right and must be followed without mentioning the use cases or contexts is blind. Be bright and wise.

Friday, August 30, 2013

"Network unlock request unsuccessful" on T-mobile Samsung Galaxy Note 2

I wanted to unlock my T-Mobile Samsung Galaxy Note 2. I requested the unlock code from T-Mobile. And I received my unlock code via email.

But when I input the unlock code into my phone, it shows "Network unlock request unsuccessful".

I called T-Mobile customer service, and they told I have to turn off the WIFI and try again. I tried that but didn't work. Then the representative said she would escalate my problem to the supervisor.

After I hanged off the call with customer service, I just realized that a couple days again I followed some instructions on the web to try to unlock my Samsung Galaxy Note 2 via a hidden menu.

The instructions look like this: DON'T DO THIS!!!
Put a SIM card from another carrier into the handset and then turn it off and back on.
Go into your dial pad and enter *#197328640#. Go to the main menu > [1] UMTS > [1] Debug Screen > [8] Phone Control > [6] Network Lock > Options [3]Perso SHA256 OFF > then you need to wait for a minute. The go into menus and select back. Now you should see ‘Network Lock’. You should choose [4] NW Lock NV Data INITIALLIZ.
There is also Youtube video that teaching you to do this. But due to my Galaxy Note 2's Android version is already too high to use this hidden menu, I don't have the [4] NW Lock NV Data INITIALLIZ option. So I gave up. But I forget to switch back to Perso SHA256 ON status.

I thought I should switch back to Perso SHA256 ON status and try to use my unlock code again. And it did work. I got my T-Mobile Samsung Galaxy Note 2 unlock with the unlock code originally received from the email.

If you played with your hidden menu before trying the unlock code, also try to switch back to Perso SHA256 ON status and try your unlock code again.

Saturday, June 29, 2013

Why VIM is so popular?

After using Vim for a half of year, I just realized why Vim is such a lovely and popular editor.

By "using Vim" I really mean use Vim as my program editor at work with a bunch of internal and external plugins. So I think get enough feeling about Vim. I also almost finish reading the great Vim book Learn Vimscript the Hard Way.

Starting from my college years to my recent software engineer career, Eclipse is always my favorite IDE, especially when I had a bunch of handy plugins installed. But as a lazy programmer, I always expected Eclipse + Plugins could be smarter to help me to finish some of the mechanical tasks. I at least tried 5 times to learn how to write a brand new or improve an existing Eclipse plugin, but I almost always stopped after I finished the Hello, World plugin. I know how important it is to grasp Eclipse plugin development as an Eclipse fan, but I failed to. It was just like I really like to read a great book, but I always stopped after reading the first chapter every time I try to start reading it.

After trying out Vim, I realized that it is so convenient to improve Vim by just a couple of lines of Vim script. Especially, after reading the great Learn Vimscript the Hard Way, I almost finish building the major trunk of entire knowledge structure of Vim. So I feel like I have a lot of power now to improve Vim as what I want.

If there was a great book name Learn Eclipse Plugin Development the Hard Way which could help me to build the knowledge structure of Eclipse Plugin architecture, I may never give Vim a chance. But now I'm no the boat of Vim, and will sail with it for a very long time I think.

In general, there are too major reasons why I switched to Vim:
  • It's more convenient to improve the function of the editor/IED.
  • It's text-based UI allowing me to work from work efficient via SSH.

Monday, June 10, 2013

Amazing Email Address Tags!

I used to work in Email industry for almost 2 years, but I even didn't know this famous and super handy  feature supported by most email service providers until recent.

It's called Address Tags. Check it out on Wikipedia, and this shorter explanation.

Framework vs. Library

From time to time, I'm wondering what's the different between framework and library in web development world, especially for Javascript frameworks and libraries:

And since recently I'm learning AngularJS (which is a JS framework, instead of just a library). And I just encountered a good explanation about the difference between framework and library from the farther of AngularJS, Miško Hevery, in his talking "Breaking Open: AngularJS".
In the framework world, the framework is in charge. It calls you when it sees necessary. You have to implement these hooks by which you get call at that point. And library is in the world the other way around. You are in full control, and you get to call the library whenever you feel like. Because the framework is the other way around, the framework has opinions. It gets to share the opinions with you, so you have to play along with the opinions the framework has. This is the simple trade you happen to have, which is that you can be more in general but do less, or you can do a lot more but you have to be opinionative, you have to do it in this particular way, or to play with the philosophies we happen to have.

Sunday, May 5, 2013

Add group symbol into numbers by Regex in Javascript

From http://stackoverflow.com/questions/6784894/add-commas-or-spaces-to-group-every-three-digits

Found (\d)(?=(\d{3})+$) is an interesting regex, here is what it can do:




'123456'.replace(/(\d)(?=(\d{3})+$)/g, '$1,')
"123,456"
'1234567890'.replace(/(\d)(?=(\d{3})+$)/g, '$1,')
"1,234,567,890"
'123456789'.replace(/(\d)(?=(\d{3})+$)/g, '$1,')
"123,456,789"


From w3schools,

  • \d Find a digit
  • n{X} Matches any string that contains a sequence of X n's
  • ?=n Matches any string that is followed by a specific string n

So (\d)(?=(\d{3})+$) breaks down to:

  • \d{3} Three digits
  • (\d{3})+ One or more group of three digits
  • (\d{3})+$ One or more group of three digits from the end of input.
  • (\d)(?=(\d{3})+$) A digit that has has the follow string which is one or more group of three digits from the end of input.

I've no idea what the performance look like for this regex, but it's quite powerful and interesting!