【发布时间】:2019-09-08 10:02:11
【问题描述】:
我一直在尝试理解,module.export 如何用它传递参数。现在我已经制作了一个演示服务器来测试它。
文件 - index.js
var express = require('express');
var check = require('./file');
var app = express();
app.get('/',check.fun1("calling"),(req,res)=>{
res.send('here i am');
})
app.listen(3000,()=>{
console.log("Server is up");
})
通过中间件检查,
module.exports = fun1 = name => {
return function(req, res, next) {
console.log(name + " from fun1");
next();
};
};
module.exports = fun2 = name2 => {
return function(req, res, next) {
console.log(name + " from fun1");
next();
};
};
现在这不起作用,但是当我更改它时,它开始起作用了
fun1 = name => {
return function(req, res, next) {
console.log(name + " from fun1");
next();
};
};
fun2 = name2 => {
return function(req, res, next) {
console.log(name + " from fun1");
next();
};
};
module.exports = {
fun1,
fun2
};
现在,这可能看起来像一个愚蠢的问题,如果它有效,那么我为什么要问,但是,我应该在 index.js 文件中进行哪些更改,以便我的第一种 module.export 类型开始工作。纯粹出于好奇
谢谢
【问题讨论】:
-
您希望
module.exports = fun1 = name => {...}做什么?fun1这里的目的是什么?如果你想从你的模块中导出两个函数,那么你写exports.fun1 = name => {...}和exports.fun2 = name2 => {...}。如果要导出单个值,则仅覆盖module.exports。不确定问题的标题与您的问题有何关系。 -
好的,我现在明白了,exports.mdoule 基本上是模块中的一个 obj,如果我将它导出两次,它就会被覆盖,嗯,有道理。但是
exports直接转换成module.export hmmm..thankx -
exports只是模块范围内的一个变量,最初引用的值与module.exports相同。