英文:
Cannot export more than one function using module.export
问题
以下是您要翻译的内容:
我尝试启动我的Node/Express应用程序时遇到以下错误。问题似乎是因为在同一文件中使用module.exports
导出多个函数引起的。也就是说,只有在导出单个函数时,应用程序才能正常启动,并且路由中间件有效。
错误:Route.get()需要回调函数,但得到了一个[object Object]。
以下是路由:
router.get('/check', MW.isAuth, function (req, res) { // 在此包括MW.otherMiddleware会引发错误
res.send({ messsage: 'Auth passed' })
})
这是中间件文件的内容。
function isAuth(req, res, next) {
const authorized = false
if (authorized) {
// 用户已经授权,调用next
console.log('Auth passed...')
next()
} else {
// 用户未被授权
res.status(401).send('You are not authorized to access this content')
}
}
function otherMiddleware(req, res, next) {
console.log('More MW operations..')
next()
}
module.exports = { isAuth, otherMiddleware }
更改为module.exports = isAuth
,或者如果我将otherMiddleware
从路由中去掉,不会导致错误发生。
如果有人能告诉我在哪里出错,我将不胜感激。
英文:
I am getting the following error when I try to start my Node/Express app. The issue appears to be caused by using module.exports
to export multiple functions from the same file. That is, the app starts fine and the route middleware works only if I export a single function.
Error: Route.get() requires a callback function but got a [object Object]
Here is the route
router.get('/check', MW.isAuth, function (req, res) { // including MW.otherMiddleware here causes error
res.send({ messsage: 'Auth passed' })
})
And this is the contents of the middleware file.
function isAuth(req, res, next) {
const authorized = false
if (authorized) {
// User is authorized, call next
console.log('Auth passed...')
next()
} else {
// User is not authorized
res.status(401).send('You are not authorized to access this content')
}
}
function otherMiddleware(req, res, next) {
console.log('More MW operations..')
next()
}
module.exports = { isAuth, otherMiddleware }
Changing to module.exports = isAuth
or if I leave the otherMiddleware
out of the route doesn't cause the error.
If anyone can tell me where I am going wrong here I'd much appreciate it.
答案1
得分: 1
你没有展示导入代码,所以错误是导入代码与导出代码不匹配,因此最终你得到的是一个中间件对象,而不是中间件函数。
如果你像这样导出:
module.exports = { isAuth, otherMiddleware };
那么你可以这样导入:
const MW = require("./middleware.js");
router.get('/check', MW.isAuth, MW.otherMiddleware, function (req, res) {
res.send({ messsage: 'Auth passed' })
});
或者,你可以使用解构赋值方式导入,如下:
const { isAuth, otherMiddleware } = require("./middleware.js");
router.get('/check', isAuth, otherMiddleware, function (req, res) {
res.send({ messsage: 'Auth passed' })
});
你之前遇到的具体错误似乎是你这样做:
const isAuth = require("./middleware.js");
这将得到module.exports
对象,而不是你的中间件函数,因此它不会是一个函数,你会得到这个错误:
Error: Route.get() requires a callback function but got a [object Object]
这个具体的错误意味着你传递了一个对象而不是一个函数给.get()
。所以你的导出中的某些部分与你的导入方式不匹配。
英文:
You don't show us the importing code so the mistake is that the import code isn't matching the export code and thus you're ending up with an object for your middleware instead of the middleware function.
If you're exporting like this:
module.exports = { isAuth, otherMiddleware };
Then, this is how you would import:
const MW = require("./middleware.js");
router.get('/check', MW.isAuth, MW.otherMiddleware, function (req, res) {
res.send({ messsage: 'Auth passed' })
});
Or, you could use destructuring assignment like this:
const { isAuth, otherMiddlware } = require("./middleware.js");
router.get('/check', isAuth, otherMiddleware, function (req, res) {
res.send({ messsage: 'Auth passed' })
});
The specific error you were getting appears like you were doing something like this:
const isAuth = require("./middleware.js");
which would get you the module.exports
object, not your middleware and thus it would not be a function and you'd get this error:
Error: Route.get() requires a callback function but got a [object Object]
That specific error means you're passing an object, not a function to .get()
. So something in your exports isn't matching up with the way you're importing.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论