In-Depth Multi-Server Configuration Tutorial

I just published an in-depth tutorial that covers how to configure a complete server stack over on the Ghost Inspector blog. I had a great experience building out Ghost Inspector’s infrastructure and wanted to share some details about what can be accomplished nowadays for just $50/mo. It’s a thorough article and walks you through the configuration of the various servers you’ll be needing: web, app, api, database, load balancer, etc. Give it a look!

Continue reading →

Sharing Javascript Between Backend (Node.js) and Frontend (Browser)

I’ve always liked the idea of being able to reuse Javascript code in both Node.js (on the backend) and the browser (on the frontend). I finally ran into a situation this weekend where it was practical to do this and I was surprised by just how easy it was to do. I thought I’d throw my solution into a post. The code below is written in CoffeeScript.

# Define my class
class myClass
  # Define a method within my class
  doSomething: ->
    console.log("Hello world!")

# Add to exports for node, or window for browser
if typeof module isnt 'undefined' and module.exports
  module.exports = new myClass()
else
  this.myClass = new myClass()

First, we create a class named "myClass" and add a method to it. Then, with a simple if statement, we can determine whether we’re running this code in Node.js or in the browser and export the class accordingly. Let’s assume we’re compiling this down into "my-class.js".

In Node.js, we would use the class like this:

myClass = require("my-class")
myClass.doSomething()

In the browser, we would use the class like this:

<script type="text/javascript" src="my-class.js"></script>
<script type="text/javascript">
window.myClass.doSomething()
</script>

Grunt Deployment over SSH with Git

Grunt Logo

Grunt.js is a task runner that comes with various plugins for compiling, building, formatting, etc. within your project. I covered some of the basics of using this tool in my article about using Grunt Watch and LiveReload for real-time compilation.

I recently setup a simple deployment process using Grunt, so I thought I’d share the details. I found a couple deployment-related Grunt plugins out there, but they didn’t really suit my needs. Instead, I opted to simply use the grunt-ssh plugin to connect to my server and run the necessary commands to update, build and restart my application. Let’s take a look at a simplified Gruntfile.coffee.

Continue reading

Node.js Async Tutorial

Node.js

I’ve been doing a lot of backend development in Node.js recently. Node.js runs on a single threaded event loop and leverages asynchronous calls for doing various things, like I/O operations. While other languages will send a database query and wait there for the result to come back, Node.js will not. When you send a database query, Node.js will continue executing the code that comes after it, then jump back when the result is available.

Continue reading