module.exports that include all functions in a single line

前端 未结 6 1003
小鲜肉
小鲜肉 2021-02-05 10:19

This is a follow-up question to In Node.js, how do I "include" functions from my other files?

I would like to include an external js file that contains common

相关标签:
6条回答
  • 2021-02-05 10:53

    It is worth noting that in ES6, you can now export functions like this:

    export function foo(){}
    export function bar(){}
    function zemba(){}
    

    Simply write export before the functions you want to export. More information here.

    0 讨论(0)
  • 2021-02-05 10:54

    A really old question but I just had to solve the same issue myself. the solution I used was to define a Class inside the module to contain all my functions and simply export an instance of the class.

    classes.js looks like this:

    class TestClass
    {
       Function1() {
            return "Function1";
        } 
        Function2() {
            return "Function2";
        }
    }
    
    module.exports = new TestClass();
    

    app.js looks like this:

    const TestClass = require("./classes");
    console.log( TestClass.Function1);
    

    just keep adding more functions to the class and they will be exported :)

    0 讨论(0)
  • 2021-02-05 10:56

    I have done something like the following:

    var Exported = {
       someFunction: function() { },
       anotherFunction: function() { },
    }
    
    module.exports = Exported;
    

    I require it in another file and I can access those functions

    var Export = require('path/to/Exported');
    Export.someFunction();
    

    This is essentially just an object with functions in it, and then you export the object.

    0 讨论(0)
  • 2021-02-05 10:59

    You can write all your function declarations first and then export them in an object:

    function bar() {
       //bar
    }
    
    function foo() {
       //foo
    }
    
    module.exports = {
        foo: foo,
        bar: bar
    };
    

    There's no magical one-liner though, you need to explicitly export the functions you want to be public.

    0 讨论(0)
  • 2021-02-05 11:05

    If you use ES6 you can do something like that:

    function bar() {
       //bar
    }
    
    function foo() {
       //foo
    }
    
    export default { bar, foo };

    0 讨论(0)
  • 2021-02-05 11:09
    const fs = require("fs")
    
    
    var ExportAll = {
    
        deleteFile : function deleteFile(image,folder="uploads"){
        
            let imagePath = `public/${folder}/${image}`
        
            if (fs.existsSync(imagePath)){
                fs.unlinkSync(imagePath)
            }
        },
    
    
        checkFile : function checkFile(image,folder="uploads"){
        
            let imagePath = `public/${folder}/${image}`
        
            if (fs.existsSync(imagePath)){
                return true
            }
            else{
                return false
            }
        },
        
    
        rand : function(min=1,max=10) 
               {
            return Math.floor((Math.random() * max) + min)
              }
    }
    
    module.exports = ExportAll
     
    
    0 讨论(0)
提交回复
热议问题