Handlebars.js Loop for helper

If you just wanted to do something n times then:

Handlebars.registerHelper('times', function(n, block) {
    var accum = '';
    for(var i = 0; i < n; ++i)
        accum += block.fn(i);
    return accum;
});

and

{{#times 10}}
    <span>{{this}}</span>
{{/times}}

If you wanted a whole for(;;) loop, then something like this:

Handlebars.registerHelper('for', function(from, to, incr, block) {
    var accum = '';
    for(var i = from; i < to; i += incr)
        accum += block.fn(i);
    return accum;
});

and

{{#for 0 10 2}}
    <span>{{this}}</span>
{{/for}}

source: http://stackoverflow.com/questions/11924452/iterating-over-basic-for-loop-using-handlebars-js

handlebars.js: Logical operator helper

Code

var hbs = require('hbs');

hbs.registerHelper({
    eq: function (v1, v2) { return v1 === v2; },
    ne: function (v1, v2) { return v1 !== v2; },
    lt: function (v1, v2) { return v1 < v2; },
    gt: function (v1, v2) { return v1 > v2; },
    lte: function (v1, v2) { return v1 <= v2; },
    gte: function (v1, v2) { return v1 >= v2; },
    and: function (v1, v2) { return v1 && v2; },
    or: function (v1, v2) { return v1 || v2; }
});

Use

{{#if (or section1 section2)}}
.. content
{{/if}}

or 

{{#if (or 
        (eq section1 "foo")
        (ne section2 "bar"))}}
.. content
{{/if}}

referent: http://stackoverflow.com/questions/8853396/logical-operator-in-a-handlebars-js-if-conditional

HTTP request logger middleware for node.js

For Install

npm i morgan -save -g

Use

// environment
var express = require("express");
var morgan = require("morgan");
var app = express();

// routing
app.get('/', function(req, res){
    res.send('<h4>mind</h4>');
});

// middleware
app.use(morgan('dev')); // for HTTP request logger

// request port
app.listen(5000,function(){
    console.log('server still running...');
});

reference: https://github.com/expressjs/morgan

Ubuntu: mongo Failed message about LANG

When you call “mongo” and found this message.

$ mongo

Failed global initialization: BadValue Invalid or no user locale set. Please ensure LANG and/or LC_* environment variables are set correctly.

You can fix by, At the command prompt:

export LC_ALL=C

and then try again. If that doesn’t work, you may need to edit:

sudo vi /etc/default/locale

and set

LANG=
LANGUAGE=
LC_ALL=

to your preferred settings. As an example:

LANG=en_US.UTF-8
LANGUAGE=en_US
LC_ALL=en_US.UTF-8