You probably didn't have a mongod running and hence it is not listening for connections. To do that you just need to open another command prompt and run mongod. After that you have to rearrange your code so that the event handler can easily connect(without any delay) the call:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
console.log("h");
});
exports.test = function(req,res) {
res.render('test');
};
So when you call mongoose.connect, it will set up a connection with the database.