Aravindan
Aravindan

Reputation: 93

how to create function like in php in node js

actually i have used functions like as follow

function selectBloodGroupNames()
{
    return array("A+", "O+", "B+", "AB+", "A-", "O-", "B-", "AB-", "A1+Ve", 
    "Unknown");
}

the above function return list of blood group which can be formatted as per our idea like to display in a drop down or else in a table!!!

i have saved that function in a separate file called functions.php

how to use the same like in node.js in express module and in ejs or basic html template engine.

Upvotes: 2

Views: 341

Answers (4)

Aravindan
Aravindan

Reputation: 93

At last i found an solution myself with the answer referencing from @IftekharDani and done some changes myself

functions.js the file which contains multiple functions!!!

module.exports = {
    menuTop: function(){
        var data = '<li>'+
        '<a href="/login">Log in</a>'+
        '</li>'+
        '<li>'+
        '<a href="/register">Register</a>'+
        '</li>';
        return data;
    },

    workCheck: function(user){
        if(user === "OLD")
        {
            var data1 = '<h1>OLD user</h1>';
        }
        else
        {
            var data1 = '<h1>Unknown or new user</h1>';
        }
        return data1;
    }
};

app.js main rendering file where i pass the entire file with the variable to the ejs file

var func = require('./functions');

app.get('/', function (req, res) {
    res.render('home', { funs : func});
});

home.ejs file that contains the function's content.

function 1

<ul class="nav navbar-nav navbar-right">
    <%- funs.menuTop(); %>
</ul>

function 2

<div class="row">
    <%- funs.workCheck("NEW"); %> 
    /* Here i pass parameter to the function that i call from the external file to which make more dynamic. */
</div>

and finally i get the expected result! i will give 50% credits to @IftekharDani for giving me an idea. Thank you!

Upvotes: 0

IftekharDani
IftekharDani

Reputation: 3729

Full Example with access array in ejs file

data.js

 const sampleArray = {
     function1(){
     return ["A+", "O+", "B+", "AB+", "A-", "O-", "B-", "AB-", "A1+Ve", 
        "Unknown"];
      },
   function2() {
     return ["A+", "O+", "B+", "AB+", "A-", "O-", "B-", "AB-", "A1+Ve", 
        "Unknown"];
     }
    }
module.exports = sampleArray;

main.js //render ejs file.

const  sampleArray = require('./data');
app.get('/renderEjs', function(req, res) {
  res.set('Content-Type', 'application/javascript');
  res.render('ejsHtmlFileName', { myArray :sampleArray.function1(),myArrayTwo:sampleArray.function2()});
});

ejsFile

<div><p><%= myArray %><%= myArrayTwo %></p></div>

Reference Link for render data in ejs file - LINK

Upvotes: 2

chel long
chel long

Reputation: 1

you should install babel to able of use ES6 for javascript

npm install --save-dev babel-core then you create separate file config.js. In config.js :

export default Config {
  selectBloodGroupNames() {
     return ["A+", "O+", "B+", "AB+", "A-", "O-", "B-", "AB-", "A1+Ve", 
    "Unknown"];
  }
}

then in file you want to use function

import Config from ./pathto/config.js;

function functionName () {
  var arr = Config.selectBloodGroupNames();
}

Upvotes: 0

Yogesh.Kathayat
Yogesh.Kathayat

Reputation: 1004

for using in node.js you can create a separate file common.js

 module.exports=function selectBloodGroupNames()
 {
    return ["A+", "O+", "B+", "AB+", "A-", "O-", "B-", "AB-", "A1+Ve", 
    "Unknown"];
 }

then in the file you want to use it you can include it and use it like below:-

   const Common=require('pathto/common.js');
   let bloodGroupList= Common.selectBloodGroupNames();
   //now you can use it where ever you want 
   console.log(bloodGroupList);  

Upvotes: 1

Related Questions