Javascript 问题导出模块NodeJS
我有以下目录结构:Javascript 问题导出模块NodeJS,javascript,node.js,Javascript,Node.js,我有以下目录结构: app/router.js app/oauth2 home-client/oauth2-client.js 资料来源: app/oauth2 home client/oauth2 client.js //SOME CODE exports.Bearer = { authenticate : passport.authenticate('bearer', { session : false }), ini
- app/router.js
- app/oauth2 home-client/oauth2-client.js
//SOME CODE
exports.Bearer = {
authenticate : passport.authenticate('bearer', { session : false }),
initialize : passport.initialize()
// session : passport.session()
};
var oauth2 = require('./oauth2-home-client/oauth2-client');
console.log(JSON.stringify(oauth2.Bearer));
//SOME CODE
app/router.js
//SOME CODE
exports.Bearer = {
authenticate : passport.authenticate('bearer', { session : false }),
initialize : passport.initialize()
// session : passport.session()
};
var oauth2 = require('./oauth2-home-client/oauth2-client');
console.log(JSON.stringify(oauth2.Bearer));
//SOME CODE
当我打印oauth2.Bearer
(以及oauth2
)内容时,我得到{}
。
我做错了什么
谢谢。您的代码:
exports.Bearer = {
authenticate : passport.authenticate('bearer', { session : false }),
initialize : passport.initialize()
// session : passport.session()
};
将导致:
exports.Bearer = {
authenticate :undefined,
initialize : undefined
};
因为passport.authenticate
和passport.initialize
返回undefined
并且具有值未定义的
的键被省略
[…]如果在转换过程中遇到未定义的函数或符号,则会将其忽略(当在对象中找到时)或删除为null(当在数组中找到时)。[…]
其值可能指向模块实例化。 你试过这个吗
module.exports = {...};
你的
router.js
在哪里?在app/
中,与oauth2 home client
文件夹的目录相同。为了清楚起见,我对我的问题进行了编辑。请尝试以下方法:var oauth2=require('oauth2-home-client/oauth2-client')代码>而不是var oauth2=require('./oauth2主客户端/oauth2客户端')
还可以尝试在JSON中的authenticate和initialize周围加上引号。您应该显示您想要对oauth2.Bearer执行的操作。目前,可以告诉您为什么会得到这个结果,但不能告诉您如何解决它。是否要执行oauth2.Bearer.authenticate()
,然后调用passport.authenticate('Bearer',{session:false})
?