Lost SSH connection after resizing (DigitalOcean)

This blog is hosted in DigitalOcean in the $5/month plan. The blog is powered by wordpress with nginx web server. I wanted have it run a MEAN stack application. After installing MongoDB, the server lost alot of performance so I decided to upgrade to the next available pricing plan, $10/month.

The resize itself was straight forward. Although I had to shutdown the server it was a pretty quick process to have the server and the blog up and running.

But for some reason ssh into it was not working. It turns out that there was an iptable entry to deny all incoming requests but there was none to accept ssh on the specified port. I had to add it the iptables:

sudo iptables -A INPUT -p tcp --dport ssh -J ACCEPT

Note that the entries might be unordered, i.e., the

DROP

 entry might be placed before the

ACCEPT

 one and this might also cause the problem.

Parsing Excel Serial date/time In Javascript

I had a case where I had to import data from an xlsx document with a bunch of columns. One of the columns was formatted as datetime and posed a problem because after the date was read, that particular column was transformed into a double ddddd.tttttt.

After reading this article a understood how to parse the serial datetime that was being returned. And because I didn’t find anything  on the net I decided to share the code I created and used:

var dt = new Date("1/1/1900");

// set the hour / minute / second
var h = workbook[0].data[i][j].toFixed(5)%1*24;
var m = h.toFixed(2)%1*60;
var s = m.toFixed(2)%1*60;

dt.setDate((dt.getDate() + workbook[0].data[i][j])-2)
dt.setHours(h.toFixed(0));
dt.setMinutes(m.toFixed(0));
dt.setSeconds(s.toFixed(0));

Hope someone finds this useful

a travel to node.js: what?

The destination is set and now we have to prepare for the journey. Where to start? What’s available? How to use it? These are all questions I had and this post is to try to shed some light on all of this.

Before we start with the ‘what is what’ I’d like to recommend that you read the original documentation of every tool presented here before searching elsewhere. Normally the original documentation has all the information one needs to get started. If I think there are other resources that complement the original documentation I’ll post here.

Node.js: it’s a javascript platform that runs on top of Chrome’s Javascript V8 runtime which makes it super fast. What makes it different is how it processes requests allowing one instance to process more requests efficiently compared to other webservers (IIS, Apache). Read through their documentation to learn more.

Node Package Manager (npm): as the name says, it manages node’s packages. It handles all the hassle of downloading, installing and loading packages and it’s dependencies. Although node comes with basic packages for http and io operations npm has a bunch of other cool stuff you’ll eventually need.

Express: it’s a web framework that runs on top of node. Although it’s super light, it comes with the necessary components to build your first “Hello World” web app.  As far as I know all other web app frameworks (meanio, compoundjs) are build on top of express.

Express View Engine/Template: Based on my research the most popular are Jade, Hoganjs (hjs), EJS but there are other templates out there that you can use. I liked Hogan because it was built by twitter (if you know Twitter Bootstrap you’ll know what I’m talking about) and it seemed performant. But it lacked documentation and because it used mustache on the background it conflicted with angular templating which I wanted to use on the frontend. Although I know I can change the delimiters I wanted as minimum configuration as possible. EJS has alot of spaguetti code which I tried to avoid from my ASP times. So I chose Jade because it’s Express default template, it seemed clean, had a bunch of cool features (partials, blocks, etc), great documentation and a lot of people use it. One drawback is that I was unable to install the textmate bundle which is a minor problem compared to the tons of features it has Found a new Textmate bundle that works great. Go through their documentation, it’s all there and it’s extremely simple to learn and use it.

nodemon: you can start your node app running:

node app

but whenever you make a change in your code like installing a new package you’ll have to stop and restart it. After a while it’s annoying and this is where nodemon come in. Whenever it detects something changed in your app it automatically restarts without you having to do anything. A useful timesaver.

Bower (Bower on GitHub): It’s a package manager similar to npm but for client side packages like angularjs, jquery, twitter bootstrap, etc. Read this for differences between bower and npm. The cool thing about this is that all 3rd party packages loaded with bower are stored on the same folder so you can separate your code from 3rd party packages.

Grunt: As the site states, it’s all about automation. Basically define automation tasks for minification, compilation, unit testing, etc. Once configured all this can be done by running the grunt command line utility without ever needing to worry about it. At first this can be a bit overwhelming because of all the configurations and setup but once it’s done you’ll be glad to have it.

Passport: it is an authentication middleware. The only reason I used it is because it has integration OAuth and OpenID and supports Facebook, Twitter and Google out-of-the-box which is helpful for certain kinds of apps. Even if you want to create your own authentication endpoint you can. Passport calls the different endpoint Strategy.

Knex & Bookshelf: Knex handles connecting, manage pooling and querying for different databases: PostgreSQL, MySQL, MariaDB and SQLite3. Bookshelf is an ORM that works side-by-side with Knex. Because of this, the same code should work with any of the databases that knex supports only modifying db (I haven’t tried it but I believe it should be the case). It is based on proven patterns supported by Backbone.js which is a plus.

Mongoose: The best way to work with MongoDB, a NoSQL database. I already worked with mongo with .NET but never did a trial run with nodejs + mongoose but from what I hear mongoose is the best tool for the job.

Bluebird (a promise library): Before you use bluebird you should understand the concept of promises. In a nutshell, using promises reduces nested code by combining into something like a fluent API. It also provides error handling bottom to top and also makes asynchronous code seem synchronous (in a non-blocking event-driven environment this is like turning rock into gold). For more detail you should read You’re Missing The Point Of Promises and Promise Nuggets. Promise anti patterns is also a good read for what not to do.

Checkit: a validation framework (if we can call it that) that validates any javascript object, be that server side (node app) or client side (browser). For example, a good way to validate user input.

Angular.js: From the likes of Backbone.js and Knockout.js angular is perfect for creating single page applications. With it’s bi-directional binding feature it takes ALOT of work out of our hands. And it is maintained by Google which is saying something! Definitely one of the best packages for building fully fledged client side applications.

Database: You probably already worked with some kind of database. It really doesn’t matter which one because from what I’ve seen node supports every single one, RDMS or NoSQL. Which database to use depends solely on your specific needs and how comfortable you are. I opted for PostgreSQL because I never worked with it (if I’m going to learn a new language might as well go all the way), it’s free which is a huge plus and, based on reviews by other developers, it has amazing features (video: Rob Conery – Five Things You Didn’t Know About PostgreSQL and, from the same author, this great article PostgreSQL Rising)

Hopefully this can shed some light on what you’ll find traveling the node path. Don’t be overwhelmed by all the information. I confess it’s alot to sink in but if you go step-by-step you’ll get the hang of it, I promise.

To make it easier I recommend you know what you want do before you try anything. Don’t try to read everything if you’re not gonna use it. Be sure were you want to go with your project, get the tools and choose the functionality to get there. In time you’ll learn all of it.

Have a nice trip to nodeland!

a travel to node.js: why?

I am using C# since I got out of the university, ie, for about 8+ years. The language is great and Microsoft ecosystem for developing and deploying web applications is extremely powerful and versatile. I have deployed web apps built solely on top of Microsoft technologies ranging from web apps based on Web Forms and MVC, RESTful services based on WCF and, most recently, WebApi. Data access layer is simply done using Entity Framework for both SQL Server and Oracle databases. All these work extremely well and are reliable.

So, despite all this good stuff why embarking on a new challenge and trying to embrace a new programming language? For three reasons:

  1. Microsoft licensing and support problems. We had problems with some of the technologies we were using and MS did not give appropriate support in order to be resolved in due time. Although we were able to find workarounds the clients were pissed off
  2. Many of the services that used to be available on self-deployed ecosystems were being deprecated because MS was/is betting big on Azure. But for our clients that was an issue because they wanted the solution to be deployed in-house and not on Azure but MS did not help with that. Again, that pissed some of the clients
  3. Some of these client’s suppliers used technologies on top of linux. That meant a considerable fewer licensing problems and costs since most of these technologies were free and the end result was almost the same. So there was pressure for us to reduce costs and be competitive or be replaced.

Because of that we had to change the strategy and move to another programming language. We were already using Objective-C and Java (Android) for developing mobile apps (smartphones and tablets). Because Objective-C is only used for developing apps for Apple ecosystem and is not geared for web development we excluded that. Java seemed the best option since we already had knowledge but I thought Java was geared to old systems, ie, it did not fit modern requirements. And, in my personal opinion, it’s not an elegant solution compared to other modern languages.

I researched further and the best bets were RubyOnRails and node.js. Ruby is an elegant language but differs quite a bit from C# which is what I’m used to. Plus the deployment process was not fully clear to me. There is passenger but, based on my readings, the free version is not targeted for large platforms. On the other hand, node.js was based on javascript which I already knew from developing web apps and the framework (based on expresss) seemed pretty elegant.

Although support and resources are widely available for both I opted for node.js because it had a lower learning curve and I think Javascript is the language of the future because it’s everywhere and many large companies are building on top of it and supporting it (Facebook, Google, Yahoo, Twitter and the list goes on and on).

I’ll be learning the in’s and out’s of node.js to assess if it fits the different scenarios we have. I’ll be sharing some of my experiences to try to help others.

bug with icon vertical align when using font-awesome and bootstrap

I tried using font-awesome, a set of icons to improve twitter bootstrap framework. It works beautifully almost every time, at least with the latest version of it (as of this writing).

For example, with the following code:

<h1 class="page-title">Map Headers</h1>

The image looked like this:

icon_off

For some reason the icon was positioned slightly up compared to to the h1 text. I compared with the past version of font-awesome and found that for some reason a piece of code was no longer there. So I created a new *.css file with the following code:

[class^="icon-"], [class*=" icon-"] {
    width: auto;
    vertical-align: baseline;
}

This will correctly position the image with the h1 text.

As a side note, when adding font-awesome you should remove the glyphicons references from bootstrap .css files to avoid conflicts between the two.

when .gitignore just ignores you!

Everyone that once worked with git already stumbled upon this problem. You added some rules to .gitignore file to be excluded in the next commits but the files still appear on the src tree. Well, the thing is that those files were previously added to the tree and .gitignore only works for new files.

You can use git clean but what that will do is physically delete the files from the disk and not just ignore them, which is what we want. The solution is clean flush the cache by running the following command:

git rm -r –cached .

And then committing again:

git commit -m “your super cool commit message” 

This will eliminate all references that were cached for .gitignore and will allow git to start from scratch.

Select a random item from a List

Hi,

Created an extension method to get a random item from a List<T>:

/// <summary>
/// Gets a random element from the list.
/// </summary>
/// <typeparam name="T">The list element type.</typeparam>
/// <param name="list">The actual list.</param>
/// <returns>An element from the list.</returns>
public static T Random<T>(this List<T> list)
{
    var r = new Random();
    return list[r.Next(0, list.Count)];
}

To use it:

var list = new List<string> { "first", "second", "third", "fourth" };
var random = list.Random();