Kill Node.js!

UPDATE: (4-24-2014) added additional ways to do this based on feedback from the community on twitter! Thanks guys!

Nope! Not what you think, or maybe hoping to find.  Remember, I am an accused “Nodejs Fanboy.”

Ever get stuck with the dreaded:

Error: listen EADDRINUSE

It can put a damper on your day, but never fear.  That error just means that another instance of that particular Node.js program is running, and you need to kill it to start it up again.  When ctrl-c doesn’t work (Mac or Linux) then you need to get all “greppy.” (Yes, greppy is a real serious technical term).

Hit the command line and type this:

ps aux | grep node

Then check for the process ID and type:

kill -9 <the process id you want to kill>

Hope you find this usefull, and remember, most of these posts are for me to remember what it was I did that “one time.”

Additional ways to do this:

A shout out to some awesome folks in the community who tweeted me alternative ways to kill Nodejs.  Thanks folks!

From: @ahmetalpbalkan   Run this command>  killall node

From: @tjanczuk   Run this command>  kill -9 $(pgrep node)

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.

Node.js and C have a conversation about the JVM ..

So I am a huge fan of Tucker and Dale vs Evil as well as being a “fanboy” of Node.js.  Well, for those who don’t know me I have an odd sense of humor, deadpan tongue and cheek.  So don’t freak out, it’s just an amusing “TO ME” thought that just ran through my head…  I have nothing against the JVM.

The part of Tucker will be played by NodeJS, while the part of Dale will be played by C.  The scene is …. (can’t give the spoiler, see the movie)

Node.js: All right… I know what this is.
C: What?
Node.js: This is a suicide pact.
C: It’s a what?
Node.js: These kids JVMs are coming out here, and killing themselves all over the woods  servers.
C: My God, that makes so much sense.

Tucker and Dale, I mean Node.js and C discuss the situation