Thursday 1 August 2013

node.js + coffeescript in a nutshell

Here's is a quick 101 on getting Node.js/CoffeeScript up on Ubuntu Server I'm using Ubuntu 13.10 that contains Node.js in its default repositories. *Note that this will not be the newest one. However it's the simplest way of getting started.

We just have to use the apt package manager. We should refresh our local list packages before installing:

sudo apt-get update

sudo apt-get install nodejs

This is all that you need to do to get set up with Node.js. You will also need to install npm(Node.js Package Manager).

sudo apt-get install npm

This will allow you to easily install modules and packages to use with Node.js.

Because of a conflict with another package, the executable from the Ubuntu repositories is called nodejs instead of node. It's just good to keep this in mind.

The last step is to install the CoffeeScript interpreter

npm install coffee-script

Now that we are setup. Here is a basic node HelloWorld server written in coffee. File:
http = require "http"
url = require "url"

start = () ->
 onRequest = (request, response) ->
  pathname = url.parse(request.url).pathname
  console.log ("Request for #{pathname}")
  response.writeHead (200),
  "Content-Type": "text/plain"

  response.write "Code Sandwich"
 http.createServer(onRequest).listen (8888)

 console.log ("Server has started.")

Ladies start your servers.


now check that this is all ok

... continue reading!

Coffee to S3

It's late, I have tron on in the background and am thinking of how to make the Grid a reality.. may not be something I can come up with tonight :/

..what else is on my mind...

I'm pushing things to Amazon's Simple Storage Service(S3) but would be good to automate them.

Coffee time! and I'm not just saying that because its late. ok, bad pun

Next we need to install the AWS SDK(more info on AWS SDK for node.js).


npm install aws-sdk
Now you will need to get your AWS access info

First login to your Amazon account at

Once logged in go to the top right, click on your name and then click "Security Credentials"

Next you will need to go to you Access Keys and click "Create New Root Key"

Next you will be prompted with the "Create Access Key". [This will invalidate your old key]
download the 'Key File' to access your newly generated key.

back to the editor and create two new files config.json and

file config.json

{ "accessKeyId": " AWSAccessKeyId goes here ", "secretAccessKey": " AWSSecretKey goes here ", "region": "us-west-2" }


#load the aws sdk
AWS = require('aws-sdk')

#load the keys to access your account
AWS.config.loadFromPath './config.json'

#lets create an object to connect to S3
s3 = new AWS.S3()

#As buckets names are shared across all account in a region
#Let create a random number so multiple people can run this example
ran = Math.random()

#call the createBucket function and add a file
 Bucket: "codemeasandwich#{ran}"
, ->
 params =
  Bucket: "codemeasandwich#{ran}"
  Key: "aFile"
  Body: "HelloWorld"

 s3.putObject params, (err, data) ->
  if err
   console.log err
   console.log data
   console.log "Successfully uploaded data to myBucket/myKey"

Now lets fire up the console and run our ""

now check that this is all ok

... continue reading!