Self-signed certs with secure WebSockets in Node.js

First generate your self-signed certs:

Then create your httpsServer from an express app using node’s built-in https server:

Then setup your websocket server (ironically this will use the same port as the http server, I didn’t know this but I guess protocols can share ports? — this had me going for awhile).

Now browse to server and accept the self-signed cert in Chrome. Then websockets should now work isnide the browser.

Open a chrome devtools console and type:

Fibonacci generator using EcmaScript 6 (es6)

I took my first stab at the --harmony flag on node 11 to play with generators.

Thanks to this video:

Here’s a simple example of using an ES6 generator to generate the first 10 numbers in the Fibonnaci sequence:

What wasn’t clear to me before watching this video by Netflix UI Architect Jafar Husain was that a generator returns an “iterator”….and on that iterator you call “.next()” to get the next yield. A generator can yield infinite times, so in this case we loop over the call 10 times to get the first 10 numbers.

Testing a node.js express app with Mocha and Should.js

I’ve been doing more Test Driven Development the past few months with my node.js application code base.

With my latest project, I decided to get started early with writing tests. While skeptical at first, I have to admit it does save quite a bit of effort in debugging and peace of mind when it comes to the stability of the code.

There are a few options with Node.js, but I decided to use the Mocha framework

and Should.js for testing my models and libraries.

Restart a Node.js app when the server reboots

I had some issues when I first setup my Debian VPS for my node.js app over at when the ISP rebooted my server a couple of times unexpectedly and my node app didn’t come back online.

I didn’t find many options out there, so I wrote a node-startup script (available on github) that can be used in /etc/init.d and will restart your node.js app when the server reboots.

First, clone the repo from github:

Edit the node-app script with your settings for node path, node environment (ie: production or development), path to application directory (where your app.js is – this is also NODE_APP variable), and a path to a pid file.

Managing config variables inside a Node.js application

In the interest of avoiding debate over this post, let’s just say there is more than one way to skin a cat when it comes to configuration management in an application. Some like to keep it out of the code base, others prefer it. This method stores the config files in the repository.

There is more than one way to handle configuration variables based on an environment for a node.js app, but I’ve come up with a pretty simple solution that keeps my config variables DRY and complete control to override them at the same time using different node environments.