Most Popular Tutorials
Most Popular Tutorials :-

Simply Easy Learning at Your Fingertips. Click Tutorials Menu to view More Tutorial List





How to create Helpers in NodeJs Application?

Create a helpers directory

File Name : helpers/helpers.js


// helpers.js
module.exports = {
get_name: function(id){
// return id * 3
if(id == '1')
{
return "India";
}
else if(id == '2')
{
return "Pakistan";
}
},

country_name: function(id){
if(id == '1')
{
return "India";
}
else if(id == '2')
{
return "Pakistan";
}
else if(id == '3')
{
return "Nepal";
}

},
}



// #############################################################
/*
module.exports = {
multiplyByTwo: function(x) { return x *2 },
divideByTwo: function(x) { return x / 2}
}
*/

/*
module.exports = function(str) {
return str.toUpperCase();
}

module.exports = {
cleanText:function(text) {
// clean it and return
},

isWithinRange(text, min, max) {
// check if text is between min and max length
}
}


exports.cleanText = function(text) {
// clean it and return
}

exports.isWithinRange = function (text, min, max) {
// check if text is between min and max length
}



module.exports = function(x) {
return {
multiply: function(y) { return y * x },
divide: function(y) { return y / x },
add: function(y) { return y + x }
}
}

*/



// ############################################################# /*

var func = {
sayhi: function(name) {
return "Hello " + name;
},
foo: function(date) {
//do somethings
}
};
module.exports = func;


// ############################################################# */

helper call in controller.

File Name : users.js


// ############## call helper.js ##############
//const helpers = require('../helpers/helpers.js');
//var kk = helpers.multiplyByTwo(10);
// console.log(kk);
// const divideByTwo = require('../helpers/helpers.js').divideByTwo(5);
// console.log(divideByTwo);
/*
const byTwo = require('../helpers/helpers.js')(2)
const byTen = require('../helpers/helpers.js')(10)
byTwo.multiply(5) // => 10
byTwo.divide(14) // => 7
byTwo.add(9) // => 11
byTen.multiply(5) // => 50
*/

// ############## end call helper.js ##############


helper call on view page.

First helper object pass in render function.

res.render('show-user-details',{
title:"Show Users Data",
data:rows,
helper: require('../helpers/helpers.js'),
});

File Name : emp_view.ejs

<%= helper.get_name(data[i].country); %>

File Name :

router.get('/show-user', function(req, res, next){
conn.query('SELECT * FROM user_details ORDER BY id desc',function(err,rows) {
if(err){
req.flash('error', err);
res.render('show-user-details',{title:"show users data",data:''});
}else{
res.render('show-user-details',{
title:"Show Users Data",
data:rows,
helper: require('../helpers/helpers.js'),
});

}
});

});

File Name :

var helper_function = require('../helpers/helpers.js');

router.get('/show-user', function(req, res, next){
conn.query('SELECT * FROM user_details ORDER BY id desc',function(err,rows) {
if(err){
req.flash('error', err);
res.render('show-user-details',{title:"show users data",data:''});
}else{
res.render('show-user-details',{
title:"Show Users Data",
data:rows,
helper: helper_function,

File Name :

<!-- <a href="/ittutorial/<%= randomProject %>"></a> -->





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here