Another Node Experiment: AndKittens

So, I know I’ve said this before, but I like to build completely useless, but fun, toys. Frankly if I let practicality be my guide I’d probably never build anything and I’m not sure how I’d learn. Please keep that in mind as you read the following. I built what follows as an excuse to practice my Node chops more, not to bring anything actually worthwhile into the Internets. Unless you like kittens. If so, this is epic. About a week or so ago I saw a tweet from Christian Heilmann talking about a new site, SynthCats. As you can probably guess, this is a site filled with pictures of cats with keyboards and synthesizers. As an unapologetic child of the 80s and new wave, I could really get behind a site like this. It got me thinking though - you could probably take any combination of cats and come up with a fun site.

From that sprouted the idea behind AndKittens. I imagined a site where you could simply enter a URL, like pugs.andkittens.us and see a collection of pictures of pugs with kittens.

Building this required a few different parts. The first was support for a wildcard domain and introspection of that domain from within Node. That was actually pretty trivial and I covered it in a quick blog post last week: Node.js Quickie: Checking the Current Domain Name.

Using that code as a stepping point, I simply introspect the subdomain and load one of two views:

The second aspect was supporting the dynamic image search. Google would be a natural fit for this, but they don’t have an API for their search service. Instead I turned to Microsoft and their Bing Search API. They made it a bit difficult to sign up and their docs were a bit hard to parse (Word docs, really? I don’t mind Word at all but I expect my API docs to be web pages!), but once I got past the initial setup it was pretty trivial to call from Node.

I wrote a quick wrapper library. Actually - I wouldn’t really call this a library as it is focused on image search only, but it could be made more generalized if folks want to take it and run. Here is the code.

Two crucial things here. First, Node.js throws an error if you try to use a https site with a certificate it doesn’t recognize. It didn’t like the Microsoft certificate and frankly, I don’t know why. I trust Microsoft though. (Really.) I used the rejectUnauthorized argument to tell Node.js to trust that I knew what I was doing and just hit the damn API. (I really wish ColdFusion would allow for this!) The second thing to remember is that Microsoft loves to use uppercase in their arguments. If you don’t match the case precisely then you will get an error. What’s odd is that they aren’t consistent. Note the $format=json part at the end.

In my opinion, unless there is some crucial performance reason it is silly to require a precise case match on arguments.

Anyway, you get the idea. That’s the back end. The front end was done by my buddy Tai. Tai is a smart cookie I’ve known for many years, but oddly he doesn’t have a blog or web site so he doesn’t get a link from me here. He designed the “viewer” aspect of the site and did the nice transitions from one image to another. Right now there is a slight bug in that if an image fails to load, we don’t immediately go to the next image (and dump it from the result set).

If you want to see the code, check out the GitHub repo here: https://github.com/cfjedimaster/AndKittens. Yes, I realize my Bing key is in there. I need to strip that out and I’ll probably max out my free key usage by the end of the month. It happens.

That’s it. Tomorrow I’m going to talk about the hosting service I used, Modulus.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for Extend by Auth0. He focuses on serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments