Friday, September 12, 2014

Express Routers as Middleware

Lately I've been developing my web applications in JavaScript, using Node with the Express framework. It's easy to get something thrown together and working very quickly, but like anything else, if you want to maintain it for long it should be well-organized.

Using Routers as Middleware is nice way of partitioning your applications into smaller, easier-to-handle modules that can even be re-used in other projects.

Sometimes you end up with a lot of different routes that have similar prefixes. In my comic application I have things such as

GET  /images
GET  /images/<image>
POST /images
GET  /data/<id>
PUT  /data/<id>
POST /data


...mixed in with a bunch of other routes. All of the /data/* routes are related, and all of the /images/* routes are related. It would be nice to extract them out into their own modules.

We can do this by creating Routers for each set of related paths and using them as Middleware in the main application.

Creating a Router is simple.

var express = require('express');
var myRouter = express.Router();

But how do you use it for something? The Router is a Middleware itself so you use it the same way you use any other Middleware.

var express = require('express');
var app = express();

var myRouter = express.Router();
app.use('/path', myRouter);

Of course, that Router won't really do anything. Let's add a path /cheese that we can GET. Here is a complete app.

var express = require('express');
var app = express();

var myRouter = express.Router();
myRouter.get('/cheese', function(req, res, next) {
    res.send(200, 'cheese is good');
});

app.use('/path', myRouter);

var server = app.listen(3000, function() {
    console.log('listening on port %d', server.address().port);
});

Now we're making progress. If you request /path/cheese you'll get the response "cheese is good." Which it certainly is.

That's great and all, but if you want to really perform some good modularization, the definition of the Router's unique functions should be moved into a separate file. So we would make a new file, cheese.js, that would look something like this.

module.exports = function(express) {

    var myRouter = express.Router();
    myRouter.get('/cheese', function(req, res, next) {
        res.send(200, 'cheese is good');
    });

    return myRouter;

};

And then your main app.js would look like this.

var express = require('express');
var cheese = require('./cheese');
var app = express();

app.use('/path', cheese(express));

var server = app.listen(3000, function() {
    console.log('listening on port %d', server.address().port);
});

And a request for /path/cheese should still tell you that cheese is good.

That's great and all, but here is the real benefit. You can add the same Router for multiple paths. Obviously, you could also reuse your cheese.js module in other applications as well. In the following example, I add the cheese router to both /path and /goat.

var express = require('express');
var cheese = require('./cheese');
var app = express();

var cheeseRouter = cheese(express);

app.use('/path', cheeseRouter);
app.use('/goat', cheeseRouter);

var server = app.listen(3000, function() {
    console.log('listening on port %d', server.address().port);
});

Now you should be able to GET /path/cheese and /goat/cheese. Goat cheese tastes much better than path cheese. Oh, what is path cheese? Well I'm sure you're familiar with leaving a trail of breadcrumbs. If you leave yourself a trail of bits of cheese you have path cheese.

My cheese example is highly simplified but if your module had a number of GETs and POSTs and stuff at different paths, the benefit of breaking it out into a separate file becomes much more clear. It also helps when some routes might need to import another module via require and no other routes need that module. Everything stays neat and self-contained.

Much easier to maintain.

Amphibian.com comic for September 12, 2014

Wednesday, September 10, 2014

Learn How to Punch Ducks

If you've come here by following the link at the bottom of today's Amphibian.com comic, you might expect me to talk about duck punching. I'm going to mention it but not really discuss it at length. Sorry, maybe some other time. If you really want to read about it, I would recommend this Paul Irish blog post from 2010. It's getting quite old now but still a good introduction to the abuse of water fowl.

What I really want to talk about is the more subtle message in the comic. That is the constant need to keep your skills up to date by learning new things. If you want to be a software engineer who remains employed long-term, you need to be constantly learning. Education is a lifelong process. Where are the people today who refused to learn C++ because they would always need FORTRAN developers? Yeah, I'm sure there are still a few FORTRAN programmers out there but not nearly as many as there are Python programmers. And don't get me started on COBOL...

If you are still reading you might ask yourself, "how does one accomplish this?" I'll tell you how not to do it, and that's expect your employer to do it for you. Odds are, you are going to be employed to develop a system in a particular language and unless it is a totally new system you'll be expected to follow some already established design patterns. And by all means, you should do that. Gotta keep the people happy who sign your paychecks. So this lifelong commitment to learning thing has to be something you do on your own time.

But here's the real key:

You need to code outside of your comfort zone.

If you are a professional Java developer like I am, learn how to create an application in Python. If you only work on back-end data processing systems, learn how to make an iPhone app. Never heard of duck punching before today? Learn Ruby and get punching. If you've only ever processed text files and displayed data in ASCII tables with dashes and vertical pipes and octothorpes and whatnot, you need to learn OpenGL.

Even if you are a Java developer for your entire career, which seems unlikely but always possible, learning how to do things in a different language will make you a better Java developer. It will make you a better software developer in general. It will make you more confident when your boss tells you that you need to move to a new project that is totally different from your last project. It will even make you smarter and more popular.

I make no guarantees of any of these claims.

But I do adhere to this myself. I think it works.

Amphibian.com comic for September 10, 2014

Monday, September 8, 2014

How Big is that Library?

Last weekend I took an old web application that had a Java back-end, a lot of hand-made JavaScript in the client, and a Node server just for websockets and converted everything to an all-JavaScript application to be run via Node.

Along the way I was also switching the JavaScript client library from Dojo to jQuery.

While the process was generally a positive one, I did learn a few things. The one I'll talk about today is the size of the client JavaScript library.

I was surprised to learn that in my case, jQuery was not a smaller client download than Dojo. If you look at their most recent versions, 1.10 for Dojo and 1.11 for jQuery, jQuery is smaller. But I was using a rather old version of Dojo, 1.4.3, which was only 87KB. The current version of jQuery is 94KB.

This got me wondering how large jQuery was back when I first made the app. I decided to perform some analysis just to satisfy my curiosity. Here is what I learned.

jQuery Version Size (KB) Release Date
1.11.0 94.1 1-23-2014
1.10.2 90.9 7-3-2013
1.9.1 90.5 2-4-2013
1.8.3 91.4 11-13-2012
1.7.2 92.6 3-21-2012
1.6.4 89.5 9-12-2011
1.5.2 83.9 3-31-2011
1.4.4 76.8 11-11-2010
1.3.2 55.9 2-19-2009
1.2.6 54.5 5-24-2008

Dojo Version Size (KB) Release Date
1.10.0 117 6-13-2014
1.9.3 115 2-20-2014
1.8.5 114 6-14-2013
1.7.4 101 10-5-2012
1.6.1 89.6 5-20-2011
1.5.1 88.2 4-12-2011
1.4.5 87.4 8-24-2010
1.3.2 79.6 7-15-2009
1.2.3 76.8 12-8-2008

The sizes I list above are for the minimized code of course. I highlighted the versions of each that were the latest when I originally made the application. At the time, jQuery was smaller but not by an enormous margin. I was more familiar with Dojo at the time, which really drove my decision to use it more than the size of the download.

The other thing I thought was interesting here was how stable jQuery has been in terms of size since 2012. The Dojo Toolkit has jumped all over the place but jQuery has been near 90KB for quite some time. In fact, you have to go back to 2009 to see a really significant change in code size.

As for the changes I had to make in my application when I switched, well, they weren't all that significant. I changed things like

dojo.byId('something')

to

$('#something')

and modified the AJAX calls a little, but there were no really major problems with the conversion. These days I just prefer working with jQuery mainly because I think the syntax is easier to remember and I have a much better experience finding useful information in their online documentation when I'm looking for something. I don't know why more products can't have online documentation as easy to use as jQuery's.

Amphibian.com comic for September 8, 2014

Friday, September 5, 2014

Your Local Bugs

Just a short note today. I found this link interesting:

Pennsylvania Insects and Bugs

You can look up insects by your state as well. I found this while trying to identify a large spider that lives by my garage door.

Have you ever walked out your door and put your face in a spider web? Yeah, it's pretty terrible.

"Hogna lenta 18" by Patrick Edwin Moran - Own work. Licensed under CC BY-SA 3.0 via Wikimedia Commons.


Amphibian.com comic for September 5, 2014

Wednesday, September 3, 2014

FCaaS (Fried Chicken as a Service)

Maybe you've heard of Software as a Service. SaaS. Maybe you've heard of Platform as a Service. PaaS. You can get just about anything as a Service these days. XaaS they call it. X stands for anything I guess.

Today's comic was about Photosynthesis as a Service. If trees could somehow turn the process of converting solar energy into chemical energy into an on-demand thing you purchased via the web, they probably wood. Sorry, I like tree puns even though they're not poplar.

Well I was reading this article about Everything as a Service. Go read it now and then come back. Really, I'll wait. Okay, now we can continue. So I read that article and I was thinking about what Colonel Sanders would do. No, that's not weird. I often think about what Colonel Sanders would do in different situations.

Here's the thing - one of the points of the "Everything as a Service" article was how the food service industry in particular is starting to embrace the outsourcing of parts of the business. Now for the Colonel Sanders part. You may or may not know that the Colonel is credited with inventing the franchise model for the fast food business. That was big at the time, but if he was starting his business today he might go with the Whatever as a Service model as his big idea.

Fried Chicken.jpg
"Fried Chicken" by Mike DelGaudio from USA - Fried Chicken.
Licensed under 
CC BY 2.0 via Wikimedia Commons.
Let's say you had a great recipe for fried chicken. The best fried chicken anyone ever tasted. You go around to some cities and train a bunch of people to cook your fried chicken in their homes. You put a website together where people can order your home-cooked fried chicken and have it delivered to them in these select locations. Someone in one of those cities goes to the website and orders some fried chicken. Seconds later, one of your trained chicken fryers in that area gets a notification on their phone that they need to fry up some food. At the same time, your system automatically sets up delivery from the home of the chicken fryer to the home of the chicken eater using one of these Car as a Service (CaaS) providers like Uber or Lyft. In 30 minutes when the chicken is done, it gets picked up and delivered. The consumer paid online when they ordered. You give the cook and the car their shares of the funds and keep the rest. The only way this could possibly get better is if you delivered the fried chicken with an aerial drone or something.

Voila! Fried Chicken as a Service. Using other things that are services.

Amphibian.com comic for September 3, 2014

Monday, September 1, 2014

Making Up Words

In addition to being a software engineer and a web comic author, I also create new words. Well, not with any sort of legitimacy I suppose. As far as I know, none of the words I've made up have ever found their way into the dictionary.

The title of today's comic is one such example.

"Verdosynthesis"

Much like photosynthesis (the suggested technology in the comic) comes from the roots "photo" and "synthesis" to mean creation of something from light, verdosynthesis is to mean the creation of something from greenness. But in this case, I am talking about the 21st century American definition of green which is of course best translated as "environmentally friendly."

Why did I use "verdo" as the first part of this word? You may be familiar with the Spanish word verde which translates to green in English. But the Spanish (and Italian) word traces back to the Latin viridis which was most likely from an even older root word that meant "growing plant." The same Latin root also carried forward into Old French as vert (which is the same word for green as in modern French - some things never change). From here, we got verdure in English which was used to describe a fresh, green color. But no one has really used that word since the 15th century. Words starting with verd- as an indication of something being green are not common in English today. There is verdigris, but that's used almost exclusively in chemistry (the green color of oxidized copper - like the Statue of Liberty) and not in the vernacular. For whatever reason, modern English uses the word green which I believe has Germanic roots.

The next question you should be asking yourself is why I know any of this. I guess you would say that I have an unusual interest in etymology. I say unusual because, seriously, how many people have an interest in etymology? Twelve? Maybe fifteen?

Also, don't confuse etymology with entomology. One is the study of word origins and the other is the study of insects. Very different. Although I suppose frogs would be more interested in entomology.

Amphibian.com comic for September 1, 2014

Friday, August 29, 2014

I'm Not Spam

Open mail relays are no joke. Except when I joke about them in my comics. But seriously, you have to be careful if you run a server to prevent spammers or botnets from using you to spam people.

When I installed postfix it listened publicly on port 25 by default. While it probably wouldn't let anyone log in to send an email, that still seemed a bit dangerous to me. I don't need somebody running a port scanner on me, seeing that I run a mail server, and then attacking me constantly trying to break in.

I had to change the following line in /etc/postfix/main.cf
inet_interfaces = 127.0.0.1
Then I had to restart postfix.

# /etc/init.d/postfix restart

It was the last line in my file. It just tells postfix to listen only on localhost and not on my public IP address. You can't connect to my server on port 25 from anywhere but my server.

SPAM and Eggs
Spam, spam, spam, spam, spam, spam, and eggs.
Alas, it did me no good. For some reason, amphibian.com is still listed as a spam site on Barracuda Central. Unfortunately, this means that many networks will block access to my comics because they assume that I am some kind of villain.

You can tell if your site is in some "bad" category by using their lookup tool. You can even request to change your category if it is wrong. I did this, but haven't gotten any response yet.

They have other informative sections of their site, where you can look at global spam statistics in graphs. The United States sends quite a bit more spam than the rest of the world, and penny stocks are an extremely popular spam subject.

While I certainly don't appreciate being labeled a spammer, the history of why unwanted email is called spam is quite interesting. It really traces back to a Monty Python sketch from 1970. In it, a couple is in a diner where every item on the menu contains Spam. The word spam is used so many times in the sketch, and Monty Python sketches are so popular among early adopters of the Internet, that people used to annoy each other in things like Usenet groups and MUDs by typing the word spam repeatedly. Just like in the sketch, the overuse of the word made it undesirable and over time the word became associated with unwanted stuff on your network, primarily email.

Amphibian.com comic for August 29, 2014