Reputation: 2796
I want to pass the console.log function to my view in Node Express, I've tried:
res.render('index', {
"title" : 'My page',
"logger": console.log
}
I've also tried this
res.render('index', {
"title" : 'My page',
"logger": function () {
console.log(arguments);
}
}
But it always comes through as empty, am I missing something?
fwiw this is how I'm trying to output it in my view
script
log = #{logger};
I develop on my iPad 2 while on the move (it's jailbroken) and Apple in their wisdom remove the develop toolbar in recent versions so I'm stuck with alerts which are just awful so I want a makeshift console.log to my terminal instead.
[EDIT]
I could subscribe on the backend to a "log" event on the front end and emit these events and data to console log.
I've not put this as an answer because I don't think it's the best way but this would work I think.
Upvotes: 0
Views: 4447
Reputation: 1484
I think you could expose the console.log() function in app.locals if you're using Express 3. See the documentation here.
So, where you configure your server you might have something like...
app.configure(function(){
// some other configuration code
app.locals.logger = function(arguments) {
console.log(arguments);
}
});
Which you could then access in your view like this...
script
var log = #{logger};
log("Hello, world!");
Which in Chrome, for example, logs "Hello, world!" to the web inspector.
Upvotes: 3