繁体   English   中英

在同一个 js 文件 node.js 中调用 exports.start

[英]calling exports.start in same js file node.js

您好,这是我在节点 js 文件中的方法:

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

如何在同一个js文件中调用这个方法? 我尝试调用 start() 和 exports.start() 但没有成功。

使用此代码:

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

要么

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

exports.start = start;

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

你可以像这样调用这个方法

    exports.start();

您可以像这样调用导出的函数

module.exports.functionName(参数);

您的命名函数:

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

然后导出对象:

module.exports = {start : start}

所以你可以在同一个js文件中调用start()

我在我的电脑上做的是以下内容,并且运行良好 - 如果您认为这是个坏主意,请发表评论!

假设你在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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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