NodeJs Tutorials
- NodeJs
- Install NodeJs
- Why use NodeJs
- NodeJs Process Model
- create First Application
- Run NodeJs Application
- Node.js Console
- Node.Js Modules
- URL Modules
- node.js Callback
- Node.js Events
- Upload Files
- Upload single & Multiple Files
- NodeJs File System
- NodeJs Email
- Debugging NodeJs
- .ENV
- NodeJs Mysql
- Helpers
- encription and decription in nodeJs
- Query string
- Date & Time
- Express Js
- Template Engine with Express
- MVC Pattern in Node.Js
- list of NPM Module
- Middleware
- Body Parser
- Render
- Nodemon module
- Morgan module
- Flash Message in ExpressJs
- Session
- Session store in database
- Cookies
- Helmet
- Multer
- Router: How To Use Routing In Node.Js
- App.Js
- express.json() and express.urlencoded()
- REST APIs in NodeJs
- Gloabal Objects
- Submit Form Data
- How to get Post Data in Node.js
- How to Get URL Parameters in Node.js
- How to create Node Project
- How to Insert Form Data Into the MySql Table Using Node.js
- How to fetch Data from MySQL database table using Node.js
- CRUD Example
- Await and Async
- Promises
- Login Example
- Password Encription
- How to validate Form (Form Validation) in Node.Js?
- Registration & Login form usingn Node.Js & MySql?
- Forgot & Reset Password
- File Upload in Node.Js with ExpressJs
- Resize Image Before Upload using Multer Sharp
- Upload multiple file using node.js with multer module
- Upload file using node.js with multer module
- Client IP Address
- REST API Downloading File in NodeJs
- Export and Download CSV From MySQL Database
- CRUD REST API
- CRUD REST API Example 2
- Enable HTTPS using Node
- How to send EMAIL using NodeJs?
- Dynamic dependent dropdown using NodeJs?
- Autocomplete Search
- Get and Send Data From Ajax Request
- Get and Post Data using Ajax
- Passport Authentication
- Node Js Data type
- Node Js Error
- Node Js Array Function
- Node Js String Function
- Puppeter Module
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> -->