Static files returning 404 in Express 3

This is a TIP to help you past a common gotcha with Express 3 static files. This does not happen in Express 4

This is a quick tip for those of you experimenting with Node.js and the Express framework.  Express allows us to create all of these wonderful rest based endpoints to give us all this great data, but sometimes you need to serve static files too.  How else are you going to get you JS, CSS, etc files to the client?

Well many people do this setup or something similar:

app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(app.router);
app.use(express.static(__dirname + '/public'));

Then they run their app and none of the static css, images, or JavaScript files are served up.  Everything 404′s on them.  What the heck is going on?

Well it’s the last two lines

app.use(app.router);
app.use(express.static(__dirname + '/public'));

You see, everyone tells you about serving static files up with this:

app.use(express.static(__dirname + '/public'));

So you put in at the end, right after:

app.use(app.router);

And that’s the problem.  You see app.use is called on app.router so now all requests will get caught by the app.router.  Everything is called in order in the app.use chain.

To fix this simply make sure you call app.use(app.router) AFTER your other app.use expressions.

app.use(express.static(__dirname + '/public'));
app.use(app.router);

Thanks so much for reading this, now upgrade to Express 4 and forget all of this, as router is not used in the middleware anymore.

Hash it out quick with Node.js (md5)

This is a quick how to in creating a simple MD5 hash in your Node.js application.  I am not recommending this for security or to be the best practice for protecting your data, so here is a little background.  Marvel comics just released an API at developer.marvel.com and one of the requirements is to pass them an MD5 hash of a timestamp, public key, and private key.  Being the geek I am I said okay and proceeded to do my thing.

This is so simple it might make you laugh.  To create an MD5 hash in Node.js… drumroll….

var data = "what ever secret I could think of";
var crypto = require('crypto');
var hashedValue = crypto.createHash('md5').update(data).digest("hex");

Sexy 6 Agile Meeting Tips for Google Hangout Usage

So, your team is remote, or maybe just out of the office for the day. Never fear, get everyone on G+ (Google+) to use the Hangout feature.

I have used this feature numerous times for standups, kickoff, estimation, and half-day planning meetings. It’s a great tool and its free. You can share your screen, see video, hear audio from everyone on the team (in the meeting of course). But with great power comes confusion.

So here are some “pro-tips*” on how to improve the experience for all your team members using the hangout feature in G+.

1 – Use headphones. By using headphones your computer speakers wont feedback into the microphone on your computer, thus eliminating that annoying echo. the annoying echo. the annoying echo
2 – You can use the microphone on your laptop but please put your laptop on a table. We dont want to hear it rubbing against your legs, or other noises from that general area.
3 – Use an external mic if you have one, if not don’t worry. This is just a “super all-pro**” move. Add a pop guard to the mic to really be a hotdog.
4 – Be aware of what is behind you, your camera will display things besides you. So clean up the moldy pizza and empty beer bottles, or just position the camera strategically to point away from the illicit activities going on in your “office”.
5 – Give everyone 10 minutes the first time they use hangouts to play with all the silly hats and overlays. It’s going to happen, don’t fight it.
6 – Pay attention. No need talking forever and being on mute. You won’t look cool, even if you have a mic and a pop guard.

So those are my Sexy 6 Agile Meeting Tips for Google Hangout Usage

As always, feel free to comment.

* The term “pro-tip” is so annoying and condescending 

** And you thought “pro-tip” was bad.