App settings on Express.js middleware

About a week ago I was wondering how to access my app settings from inside a module of my routes or some route middleware, and didn’t find the response on that moment.

Today, while I was debuging some parameters on my request the answer appeared on my eyes: both, request and response objects contain a reference to the express app :)

On my app.js :

//... Some cool lines above ...

var awsm = require('./awsm');

app.set('cool developer', 'erick');

// ... More cool lines ...

app.get('/awesome/:code', awsm.awesome);

// ... Final cool lines ...

 

And then on my awsm.js

//... Some cool lines above ...

exports.awesome = function(req, res) {
  console.log('app on req', req.app.set('cool developer'));
  console.log('and also on res', res.app.set('cool developer'));

// ... More cool lines ...
}

// ... Final cool lines ...

 

And finally, after executing the app and accessing my route, I get on my console:

app on req erick
and also on res erick

 

One useful value that is already inside settings is “env” with the current environment where the app is running.

Happy settings!

Posted in Uncategorized | Tagged , , , | 2 Comments

Underscore and Node.js

If you are like me, you should like to test your node modules on the CLI before jumping to your js files.

A couple of days ago I wanted to install and test Underscore as a Node.js module. The install was correct but then I was unable to use it on the CLI. Turns out (sorry for my ignorance by the way) that Node.js’ CLI uses the “_” variable to store information, hence I was not able to get it working untill I decided to use a different name, something like “var __”.

> var _ = require('underscore');
undefined
> _.range(10);
TypeError: Cannot call method 'range' of undefined
    at repl:1:4
    at REPLServer.eval (repl.js:80:21)
    at Interface.<anonymous> (repl.js:182:12)
    at Interface.emit (events.js:67:17)
    at Interface._onLine (readline.js:162:10)
    at Interface._line (readline.js:426:8)
    at Interface._ttyWrite (readline.js:603:14)
    at ReadStream.<anonymous> (readline.js:82:12)
    at ReadStream.emit (events.js:88:20)
    at ReadStream._emitKey (tty.js:327:10)
>

 

The above problem is ONLY for CLI, if you are on a JS file you can use the standard “_” var and everything will work like a charm!

test.js:

var _ = require('underscore');
console.log(_.range(10));

and then on my console:

node test.js

[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

 

 

Posted in Uncategorized | Tagged , , , , | 2 Comments

Developing Node.js applications on Windows

Node.js is becoming a really popular development platform. It is originally designed to work on *nix environments and most of the modules are now working properly on Windows thanks Microsoft support, but most of the modules does not necessarily means that the modules you require will actually work; one of them: Mongoose, a driver to work with MongoDBUpdate: I just found that Mongoose is already working on Windows, but this guide is still valid if you need to work with a Linux server. To solve this situation I had struggled through different configurations and tests and so far the nicest one for me is the one I’ll describe below.

Continue reading

Posted in Uncategorized | Tagged , , , , , | 7 Comments

DRAFT: Developing Node applications on Windows

So, I am starting to write a guide to work with Node in Windows. I am writing basically about some of my eperiences by getting it working on a VM on my Corporate Laptop and behind a Proxy.

http://erickrdch.com/2012/04/developing-node-js-applications-on-windows.html

Posted in Uncategorized | Tagged , , , , | Leave a comment

Rework!

Rework is an AWESOME, Eye Opener book, if you have not had a chance to read it, I highly recommend it!

Some of my favorite quotes from the book:

If you’re stuck on something for a long period of time, that means there are other things you’re not getting done.

Fried, Jason; Heinemeier Hansson, David (2010-03-02). Rework (Kindle Locations 765-766). Crown Business. Kindle Edition.

Continue reading

Posted in Uncategorized | Tagged , | Leave a comment