Search code examples
node.jsexpressmultipage

How to return array of functions from required sub page in Express, Nose.js


I build an app in node.js using Express server.
My app contain an index.js page, Dal.js page that will be able to return an array of DB function (insert,delete...). I tried to do it in this way:
*

//index.js

var express = require('express');
var app = express();
Dal=require('./Server/DB/Dal')(app);

//Dal.js

    module.exports=function(app)
    {
       var add=function(table,values, obj, next){
                 //tha action
       }

       var update=function(table,values, next){
             //tha action
       }

    **return** {
        Add:add,
        Update:update
    }
}

*

But it doesnt work!!

What is the problem??

Thanks in advance.


Solution

  • Instead of var in your class you have to to this:

    var Dal = function(){};// Init class
    Dal.prototype = { //define instance function 
       add: function(table,values, obj, next){
                 //tha action
       },
    
       update: function(table,values, next){
             //tha action
       }
    ;}
    
    module.exports=Dal; //Export your class
    

    It should work. PS: you have to create an instance (new Dal())


    Updated

    In your app.js you just have to do what you were doing

    Dal=require('./Server/DB/Dal');
    var dalInstance = new Dal();
    dalInstance.add(...);
    

    You use it like this.