Minimal Express server setup for API development

Initialise npm with defaults.

Create your main index.js entrypoint.

Install express, body-parser, morgan and winston packages.

Make your index.js look like this.

This is probably one of the most light weight node.js configuration that I have ever written for building simple REST web services.

In my opinion, this is a good starting point as it makes minimal assumptions about what you might need, letting you add whatever you need minimally on top.

Getting started with chat bots using Talkify

Chat bots have always seemed so complex. They process natural language from text so it must be hard right? After all, how can you make sense of loose words into computer instructions and then back? It must be hard.

Well, it is hard. Kinda. A lot has already been solved around natural language processing so the amount you have to do to get started has reduced by significant amount. Tools are already there, you just have to use them.

I used those tools and still found it to be difficult. I wanted to make the process of building chat bots as easy as getting started with web development. So, I built Talkify.

Takify is an Open Source framework for building chat bots. It is written in node.js and here’s how you can build your very own chat bot in a couple of minutes.

We’ll be building a chat bot that I like to call “sidekick”. This is a simple bot that tells you knock knock and chuck norris jokes. Continue reading

Why are my ExpectJS spies blocking calls?

While I love writing tests in JavaScript, it is sometimes incredibly painful to debug through the asynchronous test code. Today, a weirdness with ExpectJS happened. I had the following test code:

Simple right? Nope. Some how my callback was not being called which was preventing the test from finishing which in turn caused a test failure. I spent good amount of time debugging step by step and finally found the issue.

My fake contextStore was not being called. Hmm. How can this happen? I’ve defined functions that do respond asynchronously and have set spies on them as well. If anything, my spies should tell me that the function has been called!

Nothing could be further away from the truth! Well, you see, spies work differently in Java than in JavaScript. While using Mockito with Java, I’ve found that Spies never block the calls on the object that they are spying on unless explicitly told to. However, this behaviour different in JavaScript. ExpectJS blocks the calls to spies unless told otherwise!

At the end, solution was simple. I changed lines 11 and 12 to:

Boom! And it worked!

Handling file uploads in Express using multer

First of all, download and install multer.

Include the dependency.

Initialise it into a service. The command below will initialise it with default storage type (being DiskStorage) and upload destination being the uploads/ folder. Continue reading

Simplifying management of forks with npm forkpool module

Backstory

Recently, I’ve been working on a project that does a lot of heavy weight image processing. In a nutshell, a user uploads an image and then the website does a whole load of processing on that image and returns with the results. Depending on the size of the image it could take a while for it to load the results. The websites does other stuff as well so the scenario where a few users are concurrently uploading images, waiting for response while a whole load of other set of users are navigating to other parts of the website is very real. The performance was respectable, however, I wanted more. Continue reading