node.js on ubuntu (on linode)

I got my simple movies node.js app running through Apache using mod_proxy

$ a2enmod proxy_http
$ a2enmod proxy

Configured under sites-available like so:

$ cat /etc/apache2/sites-available/movies 
<VirtualHost *:80>
    ServerName actualfqdnofhost.bluebones.net
    ServerAlias movies.bluebones.net
    ProxyPass / http://127.0.0.1:3000/
    <Proxy *>
	Allow from all
    </Proxy>
</VirtualHost>

(This assumes that your app listens on port 3000 when running.)

Then to ensure it gets restarted when the machine does I created the following file as /etc/init/movies.conf

$ cat /etc/init/movies.conf 
description "movies.bluebones.net on node.js"
author "bakert"

start on startup
stop on shutdown

script
  export MOVIES_ROOT=/srv/movies
  export EXPRESS_ROOT=$MOVIES_ROOT/src/express
  cd $EXPRESS_ROOT && exec sudo -u www-data NODE_PATH=$NODE_PATH:/opt/local/node/lib/node_modules/ EXPRESS_ENV=production /opt/local/node/bin/node $EXPRESS_ROOT/app.js 2>&1 >>$MOVIES_ROOT/logs/node.log
end script

Which also lets me stop and start my app with:

$ start movies
$ stop movies

Set up an SPF Record to (theoretically) reduce the risk of your web app’s email being marked as spam

I’m setting up email sending from my domain instead of faking gmail.com because of mail delivery issues. To make this work without just going automatically to everyone’s spam folder I have set up an SPF record. I found the documentation on the web a bit dense trying to work this out so here’s a short look at what I did.

“Show Original” in gmail used to say:

Received-SPF: neutral (google.com: 74.54.101.112 is neither permitted nor denied by domain of bakert+fantasyfootball@gmail.com) client-ip=74.54.101.112;
Authentication-Results: mx.google.com; spf=neutral (google.com: 74.54.101.112 is neither permitted nor denied by domain of bakert+fantasyfootball@gmail.com) smtp.mail=bakert+fantasyfootball@gmail.com

Now it says:

Received-SPF: pass (google.com: domain of admin+2@ff.bluebones.net designates 178.79.178.155 as permitted sender) client-ip=178.79.178.155;
Authentication-Results: mx.google.com; spf=pass (google.com: domain of admin+2@ff.bluebones.net designates 178.79.178.155 as permitted sender) smtp.mail=admin+2@ff.bluebones.net

This means Gmail is less likely to mark email sent by my web app as spam, thanks to the SPF record I have set up.

An SPF record is implemented as a DNS TXT (as opposed to A, CNAME, etc.) record with no “name” and a value something like this:

v=spf1 a mx include:_spf.google.com ~all

Let’s break that down …

v=spf1 – this indicates that the record is an SPF v1 record.

A – this indicates that any IP address in an A record for this network is allowed to send email for this domain.

MX – this indicates that any IP address in an MX record for this network is allowed to send email for this domain.

include:_spf.google.com – this indicates that _spf.google.com is allowed to send email for this domain.

~all – this indicates that _spf.google.com is allowed to send email for this domain.

All parts of the record except v=spf1 are optional and can take a prefix of -, + or ~. If there is no prefix + is assumed. + means allowed, – means not allowed and ~ means might be allowed. all is the catch-all for anything not explicitly called out by the rule.

You might be interested in a more detailed guide to SPF record syntax.

Setup NS Records for Subdomain

If you want to serve DNS for a subdomain while keeping most of the DNS records to do with its domain with another DNS provider you need NS records.

On the nameserver for example.com:

somesubdomain			NS	ns1.otherprovider.net
somesubdomain			NS	ns2.otherprovider.net
somesubdomain			NS	ns3.otherprovider.net
somesubdomain			NS	ns4.otherprovider.net

On the otherprovider.net nameserver for somesubdomain.example.com you just need a “normal” A record (+CNAME/MX/whatever):

somesubdomain			A	178.79.178.155

I have this setup working with everydns.net providing most of my DNS but linode providing DNS for the sites I have moved off my old providers and on to linode.

The previously wonderful everydns that served me well for many years (for nothing!) has been bought up and is going paid in less than 90 days so I’m taking this opportunity to move things around.

Google’s One-Letter Guesses

Top search suggestions on google.com just now after entering a single letter:

amazon
best buy
craigslist
dictionary
ebay
facebook
google
hotmail
irs
jet blue
kohls
lowes
mapquest
netflix
osama bin laden
pandora
quotes
rebecca black
southwest
target
ups
verizon
weather
xbox
youtube
zillow
127 hours
2011 calendar
3ds
411
50 cent
60 minutes
7zip
8 mile
90210
0
@font-face
&nbsp
_
.net

“Javascript: the Good Parts”: the Good Parts

  • Use JSLint to determine if what you are doing is sensible.
  • Avoid “/* … */” for comments, “*/” appears in useful constructs in javascript. Use “//”.
  • Javascript strings are 16-bit unicode. It has no character type, only string.
  • All numbers are numeric, there is no int/float/etc.
  • The value Infinity represents all values greater than ~1.79E+308
  • Javascript has functional scoping. Braces do not denote scope. Thus, declare variables at the top of functions not within braces.
  • Falsy values: false, null, undefined, ” (empty string), 0, NaN. Everything else is truthy.
  • If using a for ... in loop you must you hasOwnProperty.
  • typeof returns "object" for an array and for null.
  • Declare var that = this; in a function so that inner functions can access that function’s this value. (Prefer “self”?)
  • Because functions are first class objects, javascript supports currying.
  • General memoizer (p. 45)
  • Don’t use new at all.
  • Functional inheritance (p. 52).
  • Do not use “/g” with RegExp.test.
  • Prefer “if (” to “if(” because “if(” looks like a method invocation.
  • K&R-style curly braces because of how return works in javascript.
  • Always pass the second parameter (radix) to parseInt.
  • Always use === and !== not == and !=.
  • Do not use with.
  • Use JSON.parse not eval.
  • Do not use ++ and --.

git

git checkout -b $BRANCHNAME origin/$BRANCHNAME
 
git checkout $PATH

git diff $ID..$ID^

git diff --cached

git log $ID

git show $ID

git commit -m "$MSG"

git log --pretty=format:'%H %an %s (%cr)' --abbrev-commit -S"$SEARCH_STRING"

git blame $ID

git status

git show :$N:$PATH

git stash; git stash apply

git bisect

git log -u

git log --grep=$SEARCH_STRING

git log --author

git log -n $N

git cherry-pick $ID

Remember the Milk “Plugin” for Alfred

Alfred is a really slick new launcher for OS X. It’s a lot simpler than QuickSilver.

One feature I can’t live without in my launcher is the ability to add tasks to my todo list and diary, both kept in the excellent Remember the Milk.

Alfred can’t run commandline programs (yet) so I wrote this simple webpage to send in tasks via the Remember the Milk email interface.

<?php

define('RTM_ADDR', 'YOUR RMILK EMAIL POST ADDRESS HERE');
define('CC_ADDR', 'IF YOU WANT TO SEND A COPY ANYWHERE');

function main() {
    $list = isset($_GET['list']) ? $_GET['list'] : 'To Do';
    $sending = $_GET['s'] . " #" . $list;
    if (strpos($sending, "^") === FALSE) {
        echo '<p>No Due Date - set to today</p>';
        $sending .= " ^today";
    }
    echo "<p>Sending " . htmlentities($sending) . "</p>";
    $success = mail(RTM_ADDR, $sending, '', "Cc: " . CC_ADDR . "\r\n");
    echo $success ? '<p style="color: green">Success</p>' : '<p style="font-size: large; color: red">Failure</p>';
}

main();

It’s quite annoying having a browser window come open and success/failure refers only to the sending of the email not the addition to Remember the Milk, but it’s a lot better than not having the capability.

PS I kind of hate the actual Remember the Milk API. It’s wonderful that they support undo and serious security and so on but it’s a real pain when all you want to do is post a task to a list and you have to deal with requesting timelines and frobs and tokens, etc.

Ning Appathon

Following last month’s Ning Apps launch, we’re excited to
announce that we’ll be holding a special developer event called the
Ning Appathon at our offices in Palo Alto, CA on Thursday,
November 5th from 6pm-10pm.

The event will include:

  • An overview of Ning Apps and our OpenSocial implementation
  • Presentations from existing Ning Apps developers
  • A chance to meet members of the Ning Engineering and Developer
    Advocacy teams
  • Free pizza and beer

Most importantly, we’ll be announcing the start of a
week-long app development competition which will include awards for
new applications in addition to ported applications. Prizes and
details will be revealed at the event.

Location

Ning
167 Hamilton Ave
2nd Floor
Palo Alto, CA 94301

Date

Thursday, November 5th

Time

6pm-10pm

Prize Info

To be announced at the event!

All attendees will receive a complimentary Ning hoodie, so be
sure to tell us your shirt size when RSVPing. You can attend solo
or bring one colleague, we only ask that you RSVP by 9pm PST on
Thursday, October 29th. All attendees must be at least 21 years
of age.

More Details and Registration