导出模块的方式

时间:2018-11-30 13:47:48

标签: javascript node.js

想象一下这段代码:

var authenticate = (req, res, next) => {
...
};

我可以像上面那样导出上面的代码吗?

module.exports = {authenticate};module.exports = authenticate;

我可以导入上述类似的代码吗? var {authenticate} = require('./middleware/authenticate');var authenticate = require('./middleware/authenticate');

1 个答案:

答案 0 :(得分:0)

这应该可以解决您的所有麻烦:https://flaviocopes.com/commonjs/

出口价值:

autheticate.js

module.exports = authenticate;

other-file.js

const authenticate = require('./autheticate.js')

或导出对象

autheticate.js

module.exports = {authenticate};

other-file.js

const {authenticate} = require('./autheticate.js')