简体   繁体   中英

calling exports.start in same js file node.js

Hi this is my method in a node js file:

exports.start = function() {
    console.log(' in start of sender.js');
});

How can I call this method in the same js file? I tried calling start() and exports.start() but not successful.

Use this code:

var start = exports.start = function() {
   console.log(' in start of sender.js');
});

or

function start() {
   console.log(' in start of sender.js');
});

exports.start = start;

//you can call start();
exports.start = function(){
    console.log('testing...')
}

You can call this method like this

    exports.start();

You can call the exported function like this

module.exports.functionName(arguments);

Your named function:

var start = function() {
   console.log(' in start of sender.js');
});

And later export object:

module.exports = {start : start}

So you can call start() in the same js file

What I'm doing on my computer is the following, and working well - please comment if you think it's a bad idea !

Let's say you're on file.js


const onThisFile = require("./file");

exports.get = async (args) => .... // whatever;
exports.put = async (args) => .... // whatever;
exports.post = async (args) => .... // whatever;
exports.delete = async (args) => .... // whatever;

exports.doSomething = async (args) => onThisFile.get(args)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM